KeyboardEvent location 属性用于返回一个数字,该数字指示键盘或设备上的键的位置。
KeyboardEvent 位置属性可用于onkeydown和onkeyup事件,但不能用于onkeypress 。
KeyboardEvent location 属性返回的数字由 4 个常量表示:
Constant | Location | Description |
---|---|---|
0 | DOM_KEY_LOCATION_STANDARD | This value represents almost every key on the keyboard, e.g. “B”, “R”, “SPACE” or “8”. |
1 | DOM_KEY_LOCATION_LEFT | This value represents a left key, e.g. the left “CTRL” key or left “ALT” key. |
2 | DOM_KEY_LOCATION_RIGHT | This value represents a right key, e.g. the right “CTRL” key or right “ALT” key. |
3 | DOM_KEY_LOCATION_NUMPAD | This value represents a numeric key or a numpad key. |
句法
event.location
下面的程序说明了 KeyboardEvent 位置属性:
示例 1:获取密钥的位置。
KeyboardEvent location Property in HTML
GeeksforGeeks
KeyboardEvent location Property
To return the location of a key,
insert some character in the field.
输出:
在按下按钮之前:
按下按钮后:
支持的浏览器:
- 歌剧
- IE浏览器
- 谷歌浏览器
- 火狐
- 苹果Safari