我有一个 Javascript 函数监听 keydown 事件并根据按下的键执行。
我的问题如下:
在 Chrome 中,当我按下“ö”时,这是一个键码为 246 的土耳其字符,event.keyCode 返回 188,这是逗号 (,) 字符的代码。 (Firefox 返回 0 但这对我来说不是问题,因为它与逗号不同。)
所以我的问题是如何区分字符“ö”和 Chrome 中的逗号?或者有没有办法在 keydown 事件中获取原始 keyCode 246?
提前致谢。
最佳答案
如果你想获得 ASCII 码,你必须监听 keypress
事件并使用 event.charCode
。
keydown
事件不提供charCode
,因为这两种事件类型之间存在一些差异:
In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.
关于javascript - event.keyCode 返回土耳其字符 "ö"的逗号 (188) 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17528838/