我已经开始使用Dojo的新on模块来添加我的事件。它很好用,但现在我遇到了一个问题。当使用keypress
事件时,我似乎无法从按键中获得字符值(例如"2"或"b")。以前我使用过behavior
模块和connect
模块,然后我可以通过使用e.keyChar
或e.charOrCode
来获得它,但现在它们是未定义的。
我有一个这样设置的事件:
on(element, 'keypress', function(e)
{
console.log(e.keyCode); //works, but not what I need
console.log(e.charOrCode); //undefined
console.log(e.keyChar); //undefined
});
使用此模块时,如何获取按键的字符?
在这种情况下,我认为您想要的是将e.keyCode
与JS原生String.fromCharCode()
结合使用,以获得所需的字符值。
on(element, 'keypress', function(e) {
var character = String.fromCharCode(e.keyCode);
if (character === 'a') { // do a stuff } else { // do something else }
}