2

我正在尝试在按下某个键时确定 javascript 中正确的键/字符代码。似乎当 CapsLock 键打开时,无法检测到小写字母。

Try the combinations:
1. a                 = a (97) shift:false
2. Shift+A           = A (65) shift:true
3. Capslock,A        = A (65) shift:false
4. Capslock, Shift+A = A (65) shift:true  -- this should be 'a'

案例 2 和 4 无法区分。

一个简单的小提琴来说明问题。 http://jsfiddle.net/sramam/pet5G/3/

OUTPUT:
keypress 'a' shift:false charCode97 keyCode:97 which:97
keypress 'A' shift:true charCode65 keyCode:65 which:65
(CAPSLOCK ON)
keypress 'A' shift:false charCode65 keyCode:65 which:65
keypress 'A' shift:true charCode65 keyCode:65 which:65

我只有一个 MacPro(Lion) 可以试一试。甚至可以检测到正确渲染的字符吗?

4

3 回答 3

2

Windows 上的按键代码是正确的,返回字母的正确按键代码,就像它会在按键上打印一样。

CAPS LOCK ON:

PRINTED 
CHARACTER       keyup/down      keypress    Modifiers
z               90              122         +Shift
a               65              97          +Shift
Z               90              90  
A               65              65

caps lock off:
Z               90              90          +Shift
A               65              65          +Shift
z               90              122 
a               65              97
于 2012-04-21T05:44:05.343 回答
2

按键检测实际上是“正确的”。您遇到的问题是在 OS X Lion 上,如果您启用大写锁定并按 shift,它会忽略大写锁定。在 Windows 上,shift + caps lock 将返回小写字母。在您的 Mac 上,它将返回大写字母。这与浏览器如何解释按键无关,而是操作系统注册的内容。

尝试输入任何应用程序,即终端,你应该明白我的意思。

这可能是其他 Mac OS 版本的情况,我在配备 OS X Lion 的 MacBook Air 上对此进行了测试。

于 2012-04-21T05:47:29.513 回答
0

所有浏览器都支持keydown、keyup和keypress事件,但是由于事件对象的keyCode属性的值从未标准化,因此存在一些互操作性问题。

// The legacy keyCode property of the keydown event object is not standardized
// But the following values seem to work for most browsers and OSes.
Keymap.keyCodeToKeyName = {
// Keys with words or arrows on them
8:"Backspace", 9:"Tab", 13:"Enter", 16:"Shift", 17:"Control", 18:"Alt",
19:"Pause", 20:"CapsLock", 27:"Esc", 32:"Spacebar", 33:"PageUp",
34:"PageDown", 35:"End", 36:"Home", 37:"Left", 38:"Up", 39:"Right", 40:"Down", 45:"Insert",  46:"Del",
// Number keys on main keyboard (not keypad)  
48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",
// Letter keys. Note that we don't distinguish upper and lower case 
65:"A", 66:"B", 67:"C", 68:"D", 69:"E", 70:"F", 71:"G", 72:"H", 73:"I", 74:"J", 75:"K", 76:"L", `   77:"M", 78:"N", 79:"O", 80:"P", 81:"Q", 82:"R", 83:"S", 84:"T", 85:"U", 86:"V", 87:"W", 88:"X", 89:"Y", 90:"Z",`

// Keypad numbers and punctuation keys. (Opera does not support these.)
96:"0",97:"1",98:"2",99:"3",100:"4",101:"5",102:"6",103:"7",104:"8",105:"9", 106:"Multiply", 107:"Add", 109:"Subtract", 110:"Decimal", 111:"Divide",
// Function keys
112:"F1", 113:"F2", 114:"F3", 115:"F4", 116:"F5", 117:"F6", 118:"F7", 119:"F8", 120:"F9", 121:"F10", 122:"F11", 123:"F12", 124:"F13", 125:"F14", 126:"F15", 127:"F16", 128:"F17", 129:"F18",  130:"F19", 131:"F20", 132:"F21", 133:"F22", 134:"F23", 135:"F24",
// Punctuation keys that don't require holding down Shift
// Hyphen is nonportable: FF returns same code as Subtract
59:";", 61:"=", 186:";", 187:"=", // Firefox and Opera return 59,61 188:",", 190:".", 191:"/", 192:"`", 219:"[", 220:"\\", 221:"]", 222:"'"
};
于 2012-04-21T05:49:12.873 回答