Skip to main content

Posts

Showing posts from March, 2011

Add a keyboard shortcut using javascript

When reading a web page,it's more comfortable for a user if that page supports keyboard shortcuts,since then a web user always no need to struggle with mouse. Add a keyboard shortcut means an event which triggers once a keyboard key is pressed.Hence the javascript event listener ' onkeyup ' is used for it. This method allow you to perform an action when a key is pressed. Each keyboard key have its own keyboard code. For example; Tab key code is 9 Enter key code is 13 Shift key code is 16 Ctrl key code is 17 Left Arrow key code is 37 We can defined our own custom keyboard keys association for a shortcut.And what we have to do is,once a user pressed keyboard keys, just need to verify that keys pressed and compare the returned value with the keyboard code associated with the keys used in our custom defined keyboard shortcut. Example: var isShift = false; document.onkeyup=function(e) { if(e.which == 16) isShift=false; }document.onkeydown=function(e){ if(e.which == 16)