Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created June 22, 2012 14:26
Show Gist options
  • Save bennadel/2973053 to your computer and use it in GitHub Desktop.
Save bennadel/2973053 to your computer and use it in GitHub Desktop.
Detecting (And Canceling) Key-Combo Events With jQuery
<!doctype>
<html>
<head>
<title>Detecting Key-Combo Events With jQuery</title>
<script type="text/javascript" src="./jquery-1.7.1.js"></script>
<script type="text/javascript">
// Run when the DOM has loaded.
$(function(){
// Let's define a generic event handler that will look at
// the key being pressed and log it out to the console.
var keyHandler = function( event ){
var keyCode = event.which;
var keyChar = String.fromCharCode( keyCode );
// Log the key captured in the event data.
console.log(
event.type + " : " + keyChar + " (" + keyCode + ")"
);
};
// Now, let's try binding both the key-down and key-press
// events to listen for the key and combos.
$( "input" ).on( "keydown keypress", keyHandler );
});
</script>
</head>
<body>
<h1>
Detecting Key-Combo Events With jQuery
</h1>
<form>
<input type="text" value="" size="30" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment