在非输入元素上反应onKeyDown/onKeyUp事件 [英] React onKeyDown/onKeyUp events on non-input elements
本文介绍了在非输入元素上反应onKeyDown/onKeyUp事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要捕获cmd按钮的上下事件,以便选择是否在setState中使用串联.我如何获得这些事件,例如在表格元素上?
I need to capture cmd button up and down events, in order to choose, whether to use concatenation or not in setState. How do i get these events, for example, on table element?
推荐答案
您必须在主体/窗口级别捕获按键.表格元素没有输入焦点,因此您无法从表格中捕获键(没有输入元素).
You have to capture the keypress then in body/window level. Table element doesn't have input focus so you can't capture the keys from table (without input element).
var cmdDown = false;
document.body.addEventListener('keydown', function(event) {
var key = event.keyCode || event.charCode || 0;
if ([91,93,224,17].indexOf(key) !== -1) {
cmdDown = true;
}
console.log('CMD DOWN: ' + cmdDown.toString());
});
document.body.addEventListener('keyup', function(event) {
var key = event.keyCode || event.charCode || 0;
if ([91,93,224,17].indexOf(key) !== -1) {
cmdDown = false;
}
console.log('CMD DOWN: ' + cmdDown.toString());
});
这篇关于在非输入元素上反应onKeyDown/onKeyUp事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文