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

Scan Codes Not Unique? #171

Open
Apollys opened this issue May 19, 2018 · 5 comments
Open

Scan Codes Not Unique? #171

Apollys opened this issue May 19, 2018 · 5 comments

Comments

@Apollys
Copy link

Apollys commented May 19, 2018

Since this issue, I've switched to using scan codes for all hotkey-related things. Things were going smoothly until...

D:\>python -m keyboard
{"event_type": "down", "scan_code": 82, "name": "insert", "time": 1526769257.152305, "is_keypad": false}
{"event_type": "up", "scan_code": 82, "name": "insert", "time": 1526769257.224113, "is_keypad": false}
{"event_type": "down", "scan_code": 82, "name": "0", "time": 1526769258.5705125, "is_keypad": true}
0{"event_type": "up", "scan_code": 82, "name": "0", "time": 1526769258.6413236, "is_keypad": true}

How do I distinguish between keys with the same "scan_code" but different "is_keypad" values in keyboard.add_hotkey() and keyboard.send()?

@boppreh
Copy link
Owner

boppreh commented May 19, 2018

In this case I blame the operating system for reporting two separate physical keys with the same scan code. Windows has "virtual key codes", and these keys do indeed report separate values. You could hack something yourself with that, but the concept is not completely portable.

I plan to add support for numpad keys in the future, so you could add_hotkey('numpad 5', ...). It could help in this case, but I'm afraid there's no good solution at the moment.

@Apollys
Copy link
Author

Apollys commented May 19, 2018

Well obviously we can distinguish the two events in the context of adding a hotkey, because the events also carry the is_keypad boolean.

In terms of sending, I'm not too familiar but again there's a clear difference, so if we could just set the is_keypad boolean in the sent event then that would handle it. I don't know if this one is easy though.

@boppreh
Copy link
Owner

boppreh commented May 19, 2018

Yes, the feature I'm looking to add is the ability of hotkey listeners to check the numpad flag.

For sending, however, things are more complicated. The numpad flag is derived from a list of signals; it's not an OS information. If you are on Windows, I suggest taking a look into the code that sends events. It's just a couple of lines long, and you can tweak to provide your own virtual key code, which does differentiate the two inserts correctly.

I know it's not ideal, but it's an easy way to fix your problem.

@boppreh
Copy link
Owner

boppreh commented May 19, 2018

Ah, one more thing. Due to a variety of situations where Windows does something dumb (e.g. scan code = 0), the library is capable of sending raw virtual key codes if you provide a negative number for the scan code. So 'send(-72)' will send an event for virtual key code 72, not scan code. You just have to check what are the virtual key codes for the keys you want.

@Apollys
Copy link
Author

Apollys commented May 19, 2018

I will look into that, thanks for the rapid feedback.

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

No branches or pull requests

2 participants