How can I wire an event to fire if someone presses the letter g?
(Where is the character map for all the letters BTW?)
How can I wire an event to fire if someone presses the letter g?
(Where is the character map for all the letters BTW?)
What about jQuery Hotkeys?
jQuery Hotkeys lets you watch for keyboard events anywhere in your code supporting almost any key combination.
To bind Ctrl+c to a function (f), for example:
$(document).bind('keydown', 'ctrl+c', f);
Well there are many ways. But I am guessing you are interested in an advanced implementation. Few days back I was in same search, and I found one.
It's good for capturing events from keyboard and you will find the character maps too. And good thing is ... it's jQuery. Check the demo on same page and decide.
An alternative library is here.
<script type="text/javascript">
$(document).ready(function(){
$("#test").keypress(function(e){
if (e.which == 103)
{
alert('g');
};
});
});
</script>
<input type="text" id="test" />
this site says 71 = g but the jQuery code above thought otherwise
Capital G = 71, lowercase is 103
You could also try the shortKeys jQuery plugin. Usage example:
$(document).shortkeys({
'g': function () { alert('g'); }
});
If you want to do it yourself, you should probably use the "key" property instead of "which" (i.e. which is deprecated):
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
$("body").on('keypress', function(e){
if('g' === e.key){
console.log("ok");
}
});
The following simple command can activate the ctrl + s key:
$(document).keydown(function (event) {
if (event.ctrlKey && event.which === 83) {
alert("Ctrl+S");
event.preventDefault();
}
});
Note: The jQuery Hotkeys plugin does not work properly