-
Notifications
You must be signed in to change notification settings - Fork 967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Trapping the 'Windows' key #81
Comments
Hey, could you be a little more specific? What keyboard command are you trying to listen for? Mousetrap supports all the modifier keys that javascript supports on keyboard events: |
Have a look here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes Apparently the char codes for Windows keys are: Left Window key: Code 91 corresponds to the |
@ccampbell, @ecgan, thanks, guys. Yes, I already inspected the char_codes purported to match the Windows key. However using a string such as 'meta+z' isn't recognized. I've also tried to mod my copy of the script to address this. I suspect the key is not assigned 91 or 92. Craig, do you know of a technique to interrogate what codes are actually mapped to keys? |
Well if the windows key doesn't map to any of the default modifier keys then it's not a simple change in mousetrap. Mousetrap doesn't keep track of anything related to which keys are down cause that is not totally reliable. I rely on the properties on the key event object (such as To answer your question (if you want to debug) you can write a simple script such as: document.addEventListener('keypress', function(e) {
console.log('KEYPRESS');
console.log('keycode', e.keyCode);
console.log('which', e.which);
console.log('meta', e.metaKey);
console.log('shift', e.shiftKey);
console.log('alt', e.altKey);
console.log('ctrl', e.ctrlKey);
console.log('');
}, false);
document.addEventListener('keydown', function(e) {
console.log('KEYDOWN');
console.log('keycode', e.keyCode);
console.log('which', e.which);
console.log('meta', e.metaKey);
console.log('shift', e.shiftKey);
console.log('alt', e.altKey);
console.log('ctrl', e.ctrlKey);
console.log('');
}, false); This output would be useful to me too for the windows key. |
Thanks! I added the script to my project and did a few quick tests. The logs are attached. It certainly looks like the char codes assigned to the LeftWindows and RightWindows keys are correct. To generate logs from the script, I had to use the Windows key in combination with another key. (Pressing the 'Windows" key alone (on Win7) displays the Start menu.) I suspect that has something to do with the results. Lastly, I see that I failed to answer your question about my intent. I'm exploring triggering a UI change based on the Windows8 keyboard shortcuts, such as Win+Z or Win+C, in pure HTML/CSS/JS. |
Unfortunately this means it is not going to be a simple thing to add to the core library since the windows key is not treated as meta, shift, ctrl, or alt. My first recommendation would be to use a different key so it works consistently cross platform/cross keyboard layouts. If that doesn't work you could try writing a plugin to do it. It's not the prettiest, but something like this could get you started: var windowsKeyDown = false;
document.addEventListener('keydown', function(e) {
if (e.keyCode == 91 || e.keyCode == 92) {
windowsKeyDown = true;
}
});
document.addEventListener('keyup', function(e) {
if (e.keyCode == 91 || e.keyCode == 92) {
windowsKeyDown = false;
}
});
Mousetrap.bind('z', function() {
if (windowsKeyDown) {
console.log('windows + z pressed');
}
}); If the appropriate keys were referenced in the _MAP dictionary at the top of mousetrap then you could use mousetrap to listen for them. |
Yeah, I actually concluded as much (and took your "first recommendation") awhile back; was just wondering if the topic had been reported/looked into. Thanks very much for your time and the library! |
This is only a question, but does the library effectively trap the so-called WINDOWS key?
I've used the library a lot (love it!) but this feature doesn't work for me.
Just wondering out loud....
The text was updated successfully, but these errors were encountered: