boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.8k stars 433 forks source link

Hotkeys not working when other keys are pressed #525

Closed Schallers closed 2 years ago

Schallers commented 2 years ago

Title. Not sure why at all, I looked through the docs to make sure there are no settings I have unchecked that are causing this, but whenever I press a hotkey with any other keys pressed, in any program, the hotkey is not activated. Any help would be appreciated.

boppreh commented 2 years ago

I'm not sure I understand the question, but if you set a hotkey for shift+a, and press ctrl+shift+a, that by design wouldn't trigger the hotkey.

If you want to listen to a key regardless of other keys being pressed, I suggest using hook_key to get all events, or on_press_key to listen only for key presses.

Schallers commented 2 years ago

Ok I guess I should go more in depth then

I want the program to have a hotkey that basically stops and starts the program. Basically like this:

add_hotkey(example, toggle_enabled()) # the function reverses the enabled boolean 
while True:
    if enabled:
        run
    else:
        continue

My issue is that, like you pointed out, by using hotkey I can only be pressing the specific button that is set if I want to activate that hotkey, however I want the user to be able to press other keys at the same time as the program is toggled aka the same time the hotkey is pressed with no issue.

I've just tried both your solutions, but now my issue is with my failsafe to the program. I'm working on a small macro system for personal use, and there is a hotkey built into the program which is used to stop the program from running. I don't know why, but with on_press_key, it instantly activates that hotkey and the program closes. I don't know if this is an issue on my end but it's really strange. I guess I should also probably add that when I made some changes to make sure it doesn't just instantly close so I could check if the hotkey that enables the program to run works at all, it did the same thing as the other hotkey, the macro is instantly activated, and trying to toggle it on or off does nothing.

boppreh commented 2 years ago

This might be your problem:

add_hotkey(example, toggle_enabled())
                                  ^^

In this example you're not registering a hotkey that will call toggle_enabled. You're calling toggle_enabled immediately, saving the returned value, then registering a hotkey that calls that returned value.

If you use it for an Exit hotkey, it'll quit immediately.

I know it was just an example, but perhaps that's the issue?

Schallers commented 2 years ago

Nah that wasn't the issue, just checked with my code, I'm using a lambda with the hotkeys, and yeah its true that when I get rid of the lambda it calls it automatically, but that wasn't my error. I'm now trying the same thing with on_press_key. Here is my exact code (also extra note, the previously working code which was correctly enabling and disabling the macro when pressed, and exiting the program when the exit hotkey was pressed, was exactly set up the same as this, except on_press_key would be replaced with add_hotkey):

keyboard.on_press_key(toggle_hotkey, lambda: toggle())
keyboard.on_press_key(quit_hotkey, lambda: quit())

In the middle of typing this I found the issue 😅 I just needed to pass in a variable for the lambda for the event handler to use, at least thats my guess? I could be extremely incorrect tho, I've never really used lambdas much

Tldr: I fixed it, the issue was in my setup of the on_press_key function, I don't think its a bug or anything. Thanks for pushing me in the right direction 😺