[英]How to detect `delete` and `.` when user press the keyboard on Chrome?
When I press .
当我按下.
it fires the three events, keydown
, keypress
and keyup
.它触发三个事件, keydown
, keypress
和keyup
。
keydown
which: 190 == ¾
keyCode: 190 == ¾
keypress
which: 46 == .
keyCode: 46 == .
keyup
which: 190 == ¾
keyCode: 190 == ¾
When I press delete
it fires two events, keydown
and keyup
.当我按下delete
时,它会触发两个事件keydown
和keyup
。
keydown
which: 46 == .
keyCode: 46 == .
keyup
which: 46 == .
keyCode: 46 == .
I want to press .
我想按.
and be able to get the corresponding character ( 46 ==.
).并能够得到对应的字符( 46 ==.
)。 But on keydown
and keyup
I get 190
which is ¾
.但是在keydown
和keyup
上,我得到190
,即¾
。 On the keypress
I get the correct value, but this event is not fired when I press delete
.在keypress
上我得到正确的值,但是当我按下delete
时不会触发这个事件。
When I press delete
I get the code 46
on keydown
and keyup
.当我按下delete
时,我在keydown
和keyup
上得到代码46
。 But the code 46
is a .
但是代码46
是.
and not delete
.而不是delete
。
How can I make an event to capture both keys and tell the difference, if it was a .
如果是.
pressed or delete
key?按下或delete
键?
Page to test: http://jsfiddle.net/Eg9F3/要测试的页面: http://jsfiddle.net/Eg9F3/
I think the best solution would be to map the keys you want ( demo ), then use e.which
to cross-reference what key was pressed.我认为最好的解决方案是 map 您想要的键(演示),然后使用e.which
交叉引用按下的键。 There is a good reference of cross-browser keyCode
values, which work in this case because jQuery normalizes the e.which
value.有一个很好的跨浏览器keyCode
值参考,在这种情况下可以使用,因为 jQuery 规范化了e.which
值。
var keys = {
46 : 'del',
190 : '.'
};
$("textarea").bind('keyup', function(e){
$('#r').append( 'keyup (' + e.which + '): ' + (keys[e.which] || String.fromCharCode(e.which)) );
});
This is similar to the method that jQuery UI uses - see the keycodes cross-reference at the top of the file?这类似于jQuery UI使用的方法 - 查看文件顶部的键码交叉引用? And it is also the method I use in the keycaster plugin.这也是我在keycaster插件中使用的方法。
In fact it's strange but it is logic.事实上,这很奇怪,但这是逻辑。
The function String.fromCharCode is working with real char code, not with KB actions (left, delete...) function String.fromCharCode 使用真正的字符代码,而不是 KB 操作(左,删除...)
Why don't filter by keyCode simply?为什么不简单地按 keyCode 过滤?
I've forced the same behavior as Firefox, example on jsFiddle我已经强制执行与 Firefox 相同的行为,例如 jsFiddle
$("textarea").keydown(function(e) {
// if the key pressed was found on the list of specialChars
if (specialChars[e.keyCode])
{
// triggers the fake event
$("textarea").trigger("chromekeypress", e);
// temporary avoid keypress from firing
$("textarea").unbind("keypress");
setTimeout(function(){ $("textarea").bind("keypress", handleKey); },10);
}
});
$("textarea").keypress(handleKey); // main event
$("textarea").bind("chromekeypress", chromeKeyPress); // chrome workaround
function chromeKeyPress(i,e){
e.type="chromekeypress";
e.which = 0; // copy Firefox behavior, which == 0 means a special key pressed
handleKey(e); // fires the main event
}
function handleKey(e) {
// which is going to be 0 if it is a special key
// and keycode will have the code of the special key
// or
// which will have the value of the key
$("#r").html($("#r").html() + "\n" +
e.type + "\n" +
" which: " + e.which + " == " + String.fromCharCode(e.which) + "\n" +
" keyCode: " + e.keyCode + " == " + String.fromCharCode(e.keyCode) + "\n" +
"");
}
The value of "which" in a "keypress" handler does not have the same meaning as that of "which" in a "keydown" or "keyup" handler. “keypress”处理程序中“which”的值与“keydown”或“keyup”处理程序中的“which”的含义不同。 In those, it's the keycode for the key on the keyboard, and not a character ordinal.其中,它是键盘上键的键码,而不是字符序数。 In "keypress" it is the character, but you don't get a "keypress" for Del
(as you've noticed).在“按键”中,它是角色,但您没有得到Del
的“按键”(正如您所注意到的)。
Thus, that's a different 46 in the "keypress" handler (for ".") than in the "keydown" and "keyup" events for Del
.因此,“keypress”处理程序(用于“.”)中的 46 与Del
的“keydown”和“keyup”事件中的 46 不同。
You probably should use either "keydown" or "keyup" and check for the keycode.您可能应该使用“keydown”或“keyup”并检查键码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.