Skip to content
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

Closed
netaisllc opened this issue Dec 20, 2012 · 7 comments
Closed

Question: Trapping the 'Windows' key #81

netaisllc opened this issue Dec 20, 2012 · 7 comments

Comments

@netaisllc
Copy link

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....

@ccampbell
Copy link
Owner

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: shift, ctrl, alt, and meta. Not sure if the windows key is one of those.

@ecgan
Copy link

ecgan commented Dec 21, 2012

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: 91
Right Window key: 92

Code 91 corresponds to the meta key mentioned by @ccampbell , meaning the Left Window key should work. However, Right Window key (code 92) is not in mousetrap.js.

@netaisllc
Copy link
Author

@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?

@ccampbell
Copy link
Owner

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 e.metaKey). This means mousetrap doesn't let you do keyboard combinations such as a+b. This was a conscious decision.

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.

@netaisllc
Copy link
Author

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.

12-21-2012 12-20-33 PM

@ccampbell
Copy link
Owner

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.

@netaisllc
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants