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

add_hotkey keeps bound keys pressed throughout the callback function #135

Closed
giladbarnea opened this issue Mar 8, 2018 · 4 comments
Closed

Comments

@giladbarnea
Copy link

Hi! Thanks for this lib. I'm using it pretty extensively.

I nailed down exactly the situations where this bug occurs, and when it doesn't.
An example for the bug: Given hotkey: 'ctrl+l', sending 'alt' in the callback fn will send ctrl+alt+l, even if 'ctrl+l' was physically released, trigger_on_release is set to True, as so is suppress.

I checked that via is_pressed.

  1. In the the attached file there are 3 main functions (0,1,2) and 4 groups (A,B,C,D) that check those functions from different angles.
  2. The functions print whether ctrl is_pressed, with different minor changes between them.
  3. The groups add elements like calling the functions in a different thread, sleeping or releasing the hotkey etc.
  4. The lines are commented because multiple ones are binding the same hotkey, so please uncomment them as needed.
  5. The summary of each group is written inside a tri-quote above it.

Results:

  • I tested on the PyPi lib, the master github branch and the suppress branch.
  • The master branch and the suppress branch give identical results, so let's refer to them as the repo from now on.
  • To bypass the bug, PyPi needs a different thread (call_later), and: either some sleep before checking is_pressed, or a manual keyboard.release. Both are sufficient.
  • The repo needs a call_later and a sleep ONLY. Manually releasing doesn't have any effect.

reproduce.txt

Hopefully that sheds some light on the issue!

Thanks again,
Gilad

@boppreh
Copy link
Owner

boppreh commented Mar 29, 2018

(Sorry for the responding earlier, I had typed an extensive reply but apparently failed to send.)

I'm not sure I understand 100% the issue, so let me try to reword it; the following snippet currently prints True when releasing ctrl+a, when it should return False? And the attached file shows different ways to work around this problem?

import keyboard
keyboard.add_hotkey('ctrl+a', lambda: print(keyboard.is_pressed('a')), suppress=True, trigger_on_release=True)

Because that does sound like a bug.

Note:

  • this doesn't mean that sending alt will send ctrl+alt+l. Because the ctrl+l have been suppressed, other applications will see only alt.
  • is_pressed returns the physical status of a key (to its best ability). Faking key events should never affect the result. Also, other applications may disagree when an event is suppressed.
  • callbacks set with suppress will always be executed before the event is processed by other applications, even with trigger_on_release, due to the nature of event suppression.

@boppreh
Copy link
Owner

boppreh commented Mar 29, 2018

If my understand of the issue is correct, can you test if the commit above fixes it?

@giladbarnea
Copy link
Author

Yes! it does.
Sorry for taking such a long time. The bug is indeed fixed.
Thanks.

@boppreh
Copy link
Owner

boppreh commented Apr 27, 2018

Awesome, thank you for testing.

@boppreh boppreh closed this as completed Apr 27, 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

2 participants