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

Alt keys aren't detected properly #48

Closed
Hyphen-ated opened this issue Feb 4, 2017 · 12 comments
Closed

Alt keys aren't detected properly #48

Hyphen-ated opened this issue Feb 4, 2017 · 12 comments

Comments

@Hyphen-ated
Copy link

As mentioned in a thread about a different issue:

When I run this program:

import keyboard
def info(event):
    print (event.name + ", " + str(event.scan_code) + ", " + event.event_type)
keyboard.hook(info)
keyboard.wait("esc")

and press left alt, I see "left alt, 56, down" as expected. When I release left alt, I see
left alt, 56, up
left alt, 56, down
left alt, 56, up

When I press and release right alt, I see:
left alt, 56, down
left alt, 56, up

I went looking for someplace else to test my keyboard events, and found https://w3c.github.io/uievents/tools/key-event-viewer.html
When I press and release left alt and then right alt in this tool, it looks like this:
http://i.imgur.com/FNNKpZn.png
It's not showing either problem.

I was slightly surprised that it was reporting my numlock status in this other tool. I tried turning off numlock to see if that made any difference to my "keyboard" problem. It didn't.

Windows 8.1
Tested on Python 2.7.13 and 3.6.0
keyboard 0.9.12

@Hyphen-ated
Copy link
Author

I inserted

print "{} {} {} {}".format(event_type, vk, scan_code, is_extended)

at https://github.com/boppreh/keyboard/blob/master/keyboard/_winkeyboard.py#L368

When I press left alt, this prints
down 164 56 0
up 164 56 0
down 164 56 0
up 164 56 0

When I press right alt, this prints
down 165 56 1
up 165 56 1
down 164 56 0
up 164 56 0

When I press any other key, it only prints two lines.
I'm using a US keyboard.

@Hyphen-ated
Copy link
Author

Hyphen-ated commented Feb 5, 2017

The alt key is kind of weird, so maybe it's okay to just say "you know what, you can't bind things directly to the alt key".
But I think it's pretty important for key combos involving alt to work.

def okay():
    print "okay"
keyboard.add_hotkey('alt+q', okay)

This hotkey is triggered once when I press alt+q with the left alt key. It is not triggered when I use the right alt key. In other applications, alt shortcuts work just fine using either alt key.

boppreh added a commit that referenced this issue Aug 27, 2017
This should help with #48, where the alt key was appearing double. In my
computer a single click of 'alt' sent 'down, up, down', leaving the key
in a pressed state. The reporter complains about a slightly different
problem, but it's likely connected.

I noticed the second key had the INJECTED flag, so this patch ignores
injected events. Still have to watch out for unintended consequences.

Note that I'm keeping the _listener.is_replaying code that tries to
ignore replayed keys, because this fix is Windows-only.
@Enteleform
Copy link
Contributor

Enteleform commented Sep 22, 2017

OS: Windows 10.0.15063 x64
Python: 3.6.2
Keyboard: Lenovo ThinkPad Compact USB Keyboard

My left-alt key is detected correctly, but the right-alt key is not detected at all.
It is, however, detected by the online key-event-viewer utility that @Hyphen-ated linked to.

All other modifiers work as expected and work on both left & right sides.

Tested with keyboard.hook and keyboard.add_hotkey.

@boppreh
Copy link
Owner

boppreh commented Feb 25, 2018

I could never reproduce the issue, but detection of alt gr has been greatly improved in the latest version. If anyone who is having this issue could try the master branch, I would greatly appreciate.

@Hyphen-ated
Copy link
Author

When I run my two test programs from the top of this thread on master, pressing left alt makes them correctly print

alt, 56, down
alt, 56, up

and

down 164 56 0
up 164 56 0

respectively.

Pressing right alt makes them both print nothing.

@boppreh
Copy link
Owner

boppreh commented Feb 25, 2018

Thank you for the quick answer!

Is your right alt a normal alt key, or an alt gr? Does it work on other applications? Sometimes keys are not mapped at all.

@Hyphen-ated
Copy link
Author

Hyphen-ated commented Feb 25, 2018

It's a normal alt key and it works in other applications. It's the same OS and hardware setup as the first two posts in this thread

@boppreh
Copy link
Owner

boppreh commented Feb 25, 2018

I've looked everywhere and there's only one thing left to test: on _winkeyboard.py there's a check if vk == 165: return True (currently line 493) to skip a duplicated event in case of AltGr. Can you try commenting out that check to see if right alt prints something?

@Hyphen-ated
Copy link
Author

Hyphen-ated commented Feb 25, 2018

When I remove that check, I get what looks like it might be correct behavior (I'm not sure about what that 1 at the end instead of 0 for "is_extended" means exactly):

right alt, 56, down
right alt, 56, up

and

down 165 56 1
up 165 56 1

@boppreh
Copy link
Owner

boppreh commented Mar 29, 2018

I'm revisiting old issues, and I think I found a better (but still not elegant) solution to this problem.

Summary of the problem:

  1. If a keyboard has an alt-gr key, pressing it emits two interleaved events: alt gr and right alt.
  2. If a keyboard only has a right alt key, it only emits a right alt event.

Number 1. is a problem because it makes holding alt gr look like it applies two different modifiers, making a hotkey like alt gr+a not match. The previous hack was to completely discard any events with vk 165 (right alt), because I didn't know any keyboards that had such a key, and at the time it was reporting an incorrect name (menu).

What commit 9a462d2 does is to still discard events with vk 165, but only if they follow an alt gr event. So, still a hack, but closer to the truth. Alt-gr keyboards should report only a single alt-gr event, while right-alt keyboards still pass through.

I need someone to test this solution (available on the master branch) with a keyboard that has a right alt. Until then I'm keeping the issue open, but hopefully solved.

@Hyphen-ated
Copy link
Author

Hyphen-ated commented Mar 30, 2018

I checked out master and ran the program from the top of this thread. When I press+release left alt, then right alt, I get this:
alt, 56, down
alt, 56, up
right alt, 56, down
right alt, 56, up

This looks correct

@boppreh
Copy link
Owner

boppreh commented Mar 30, 2018

Great, thank you. I'm finally closing this issue, but feel free to comment on it again and I'll reopen if you think some problem persists.

@boppreh boppreh closed this as completed Mar 30, 2018
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

3 participants