I want to simulate GMail/Twitter keyboard shortcuts of pressing a key, followed by another key to navigate somewhere in jQuery.
For example, G then H to go "Home" in GMail. Twitter also has G then H and other "bo" shortcuts.
Is there a jQuery plugin that simulates this behaviour?
I want to simulate GMail/Twitter keyboard shortcuts of pressing a key, followed by another key to navigate somewhere in jQuery.
For example, G then H to go "Home" in GMail. Twitter also has G then H and other "bo" shortcuts.
Is there a jQuery plugin that simulates this behaviour?
Share Improve this question asked Dec 19, 2011 at 4:53 Brad LeachBrad Leach 17k18 gold badges74 silver badges88 bronze badges 2- You mean something like this : github./jeresig/jquery.hotkeys help...? – Sudhir Bastakoti Commented Dec 19, 2011 at 5:01
- I haven't seen a way to have bo shortcuts with the jquery.hotkeys plugin. You can do multiple keypresses (like Ctrl+A), but not G, H. If I am mistaken, I would be very pleased, as I already want to use this plugin. – Brad Leach Commented Dec 19, 2011 at 5:03
4 Answers
Reset to default 8I just released a library last week called mousetrap that has this functionality built in. Check out http://craig.is/killing/mice
You can do
Mousetrap.bind('g h', function() {
console.log('go home!');
});
Not sure about plugins, but you can achieve this other wise by keeping a key queue and processing that queue on each mouseup.
i.e. Something like:
var myKeyQueue = [];
$(document).keydown(function(e) {
var code e.charCode != 0 ? e.charCode : e.keyCode;
myKeyQueue.push(code);
});
$(document).keyup(function(e) {
processKeyQueue();
});
And you processKeyQueue function will be something like:
function processKeyQueue() {
// check if the Q contains any actionable key-bo, 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
}
}
Check out this Doing key bos with jQuery / JavaScript I think that it will answer your problem. Also this guy wrote his own VERY small plugin to handle CTRL+[Any Key] listener so that he could use shortcuts like CTRL+S to call a function that saves the active document, it might be able to be modified for your use
I think this: http://www.openjs./scripts/events/keyboard_shortcuts/ might help in some way for adding your own shotcuts