KeyboardEvent which 属性
定义和用法
which 属性返回触发 onkeypress ɗanar 的按键的 Unicode 字符码,或触发 onkeydown or onkeyup 事件的按键的 Unicode 按键代码。
两种代码类型的区别:
- 字符代码 - 代表 ASCII 字符的数字
- 按键代码 - 代表键盘上实际键的数字
这些类型并不总是意味着同样的事情。例如,小写 "w" 和大写 "W" 的键盘代码相同,因为在键盘上按下的键是相同的("W" = 数字 "87"),但字符代码不同,因为 resulting 字符是不同的("w" 或 "W",即 "119" 或 "87")- 请查看下面的更多实例,可以对这一点有更好的理解。
Hint:如需了解用户是否按下了可打印的键(例如 "a" 或 "5"),建议在 onkeypress 事件上使用此属性。要了解用户是否按下了功能键(例如 "F1"、"CAPS LOCK" 或 "Home"),请使用 onkeydown 或 onkeyup 事件。
Note:IE8 and earlier versions do not support the which attribute. For these browser versions, you can use the keyCode attribute. However, the keyCode attribute does not work for the onkeypress event in Firefox. For a cross-browser solution, you can use the following code:
var x = event.which || event.keyCode; // Use which or keyCode depending on browser support
Hint:For a list of all Unicode characters, see our Complete Unicode reference.
Hint:If you need to convert a Unicode value to a character, use fromCharCode() method.
Note:This attribute is read-only.
Note:The keyCode and which attributes are provided for compatibility purposes only. The latest version of the DOM event specification recommends using the key attribute instead (if available).
Hint:If you want to know if the "ALT", "CTRL", "META", or "SHIFT" keys were pressed at the time of the key event, use altKey,ctrlKey,metaKey or shiftKey Attribute.
مثال
Get the Unicode value of the pressed keyboard key:
var x = event.which;
More TIY examples are below the page.
Syntax
event.which
Technical details
Return value: | Numeric value, representing Unicode character code or Unicode keyboard code. |
---|---|
DOM version: | DOM Level 2 Events |
Browser support
Number in table indicate the first browser version that fully supports the attribute.
Attribute | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
which | Girmi | 9.0 | Girmi | Girmi | Girmi |
Maiharin dakeci
مثال
A gina ce gina onkeypress da onkeydown domin kama kura dakeci kuma code na bai na kai na keyboard:
<input type="text" onkeypress="uniCharCode(event)" onkeydown="uniKeyCode(event)"> function uniCharCode(event) { var char = event.which || event.keyCode; // event.keyCode مخصص لـ IE8 وكل الأصدار السابقة document.getElementById("demo").innerHTML = "كود CHARACTER Unicode: " + char; } function uniKeyCode(event) { var key = event.which || event.keyCode; // event.keyCode مخصص لـ IE8 وكل الأصدار السابقة document.getElementById("demo2").innerHTML = "كود KEY Unicode: " + key; }
عند ضغط مفتاح "a" على لوحة المفاتيح (بدون استخدام Capslock)، ستكون النتائج char وkey كالتالي:
كود CHARACTER Unicode: 97 كود KEY Unicode: 65
مثال
إذا ضغط المستخدم على مفتاح الإفلات، فسيتم عرض بعض النصوص:
<input type="text" onkeydown="myFunction(event)"> function myFunction(event) { var x = event.which || event.keyCode; // event.keyCode مخصص لـ IE8 وكل الأصدار السابقة if (x == 27) { // 27 هو مفتاح الإفلات alert ("أضغطت على مفتاح الإفلات!"); } }
مثال
تحويل القيمة Unicode إلى حرف (لا ينطبق على الأزرار الوظيفية):
var x = event.which || event.keyCode; // الحصول على القيمة Unicode var y = String.fromCharCode(x); // تحويل القيمة إلى حرف
الصفحات ذات الصلة
دليل مرجع HTML DOM:KeyboardEvent key ɗanar
دليل مرجع HTML DOM:KeyboardEvent keyCode ɗanar
دليل مرجع HTML DOM:KeyboardEvent charCode ɗanar