Javascript 处理组合键盘快捷键
我想模拟GMail/Twitter的键盘快捷键,按下一个键,然后再按另一个键,在jQuery中导航 例如,G然后H在GMail中返回“家”。Twitter也有G、H和其他“组合”快捷方式Javascript 处理组合键盘快捷键,javascript,jquery,Javascript,Jquery,我想模拟GMail/Twitter的键盘快捷键,按下一个键,然后再按另一个键,在jQuery中导航 例如,G然后H在GMail中返回“家”。Twitter也有G、H和其他“组合”快捷方式 有没有一个jQuery插件可以模拟这种行为?看看这个,我想它会回答你的问题。他还编写了自己的非常小的插件来处理CTRL+[Any Key]侦听器,这样他就可以使用快捷键(如CTRL+S)来调用保存活动文档的函数,它可能可以被修改以供您使用,但对插件不太清楚,但您可以通过保留一个键队列并在每个鼠标上处理该队列来实
有没有一个jQuery插件可以模拟这种行为?看看这个,我想它会回答你的问题。他还编写了自己的非常小的插件来处理CTRL+[Any Key]侦听器,这样他就可以使用快捷键(如CTRL+S)来调用保存活动文档的函数,它可能可以被修改以供您使用,但对插件不太清楚,但您可以通过保留一个键队列并在每个鼠标上处理该队列来实现这一点 i、 e.类似于:
var myKeyQueue = [];
$(document).keydown(function(e) {
var code e.charCode != 0 ? e.charCode : e.keyCode;
myKeyQueue.push(code);
});
$(document).keyup(function(e) {
processKeyQueue();
});
function processKeyQueue() {
// check if the Q contains any actionable key-combo, if so do it!
/*
if(myKeyQueue.length == 2) {
if(String.fromCharCode(myKeyQueue[0]) == 'G' && String.fromCharCode(myKeyQueue[0]) == 'H') {
// you code here ...
}
}
*/
if(myKeyQueue.length >= 2) {
myKeyQueue = []; // remove all keys in Q
}
}
您可以使用processKeyQueue函数:
var myKeyQueue = [];
$(document).keydown(function(e) {
var code e.charCode != 0 ? e.charCode : e.keyCode;
myKeyQueue.push(code);
});
$(document).keyup(function(e) {
processKeyQueue();
});
function processKeyQueue() {
// check if the Q contains any actionable key-combo, if so do it!
/*
if(myKeyQueue.length == 2) {
if(String.fromCharCode(myKeyQueue[0]) == 'G' && String.fromCharCode(myKeyQueue[0]) == 'H') {
// you code here ...
}
}
*/
if(myKeyQueue.length >= 2) {
myKeyQueue = []; // remove all keys in Q
}
}
我认为:
可能在某种程度上有助于添加您自己的快照我上周刚刚发布了一个名为mousetrap的库,其中内置了此功能。退房 你能行
Mousetrap.bind('g h', function() {
console.log('go home!');
});
你的意思是这样的:帮助…?我还没有看到一种通过jquery.hotkeys插件实现组合快捷方式的方法。你可以按多个键(比如Ctrl+A),但不能按G、H。如果我弄错了,我会非常高兴,因为我已经想用这个plugin.Tyvm来制作这个很棒的库了!