RSS

|| javascript || tutorial || bind events with keyboard shortcuts

19 Oct

For relating keyboard short cuts with checking or unchecking the checkbox, i once need to get the value of key pressed. Following is the simple code for getting the ascii of any key pressed.

<script type="text/javascript">
$(document).ready(function() {
jQuery(document).keydown(function(e) {
alert(e.which);
});
});
</script>

For binding event with multiple keys like Ctrl+a, following code is there to help you out,

<script type="text/javascript">
$(document).ready(function() {
var ctrl = false;
jQuery(document).keydown(function(e) {
if(e.which == 17) //17 = ctrl
{
ctrl = true;
}
if(e.which == 65 && ctrl) //65 = a
{
alert("You have pressed Ctrl + a");
}
});
});
</script>

Thats it 🙂

Advertisements
 
Leave a comment

Posted by on October 19, 2011 in javascript, php

 

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: