How To Implement Shortcut Key Combination Of Ctrl Or Shift + Through Javascript?
Solution 1:
Your event listener function, gets passed an Event object. That has a lot of useful information on it, including the properties "altKey", "ctrlKey", "shiftKey" and "metaKey". If any of the modifier keys are being held down when that event fires, the corresponding property is set to true.
This applies to keyboard as well as mouse events (onclick, etc). Note that if you have a onkeydown event listener, the modifier key itself will fire the event.
window.onkeyup = function(e) {
if (e.altKey) alert("Alt pressed");
if (e.shiftKey) alert("Shift pressed");
}
This tested on Firefox 3, Windows XP.
Solution 2:
Javascript has support for ctrl+alt+shift keys. I assume you can figure out the rest. Link.
Solution 3:
I know this is not answering the orginal question, but here is my advice: Don't Use Key Combination Shortcuts In A Web Application!
Why? Because it might break de the usability, instead of increasing it. While it's generally accepted that "one-key shortcut" are not used in common browsers (Opera remove it as default from its last major version), you cannot figure out what are, nor what will be, the key combination shortcuts used by various browser.
Moreover, if your visitors use a higly customisable browser, such as Firefox or Opera, There is a high risk that they have either configure their own shortcuts or use additional plug-ins that define some additional ones.
Keep in mind that web applications are browser dependend and that you should NEVER assumes that if it works on your favorite-tweak-most-powerfull-browser, it will react the same way on your friend's one.
Post a Comment for "How To Implement Shortcut Key Combination Of Ctrl Or Shift + Through Javascript?"