moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

Using shift_l hotkey (KeyCode = 50) #396

Closed wolf0001 closed 3 years ago

wolf0001 commented 3 years ago

I would like to use the "shift_l", "ctrl_l" and "alt_l" hotkeys. Here's my code which works:

with keyboard.GlobalHotKeys({ 'f': function_1, 's': function_2, 'q': function_3, '': function_4, '': function_5, '': function_6}) as h: h.join()

When I substitute '' and run the code both shifts still trigger when I want only the left shift to trigger. For '' and '' nothing happens. Can I substitute the keycodes here and how? Thanks

moses-palmer commented 3 years ago

Thank you for your report.

This is not possible, as modifiers---alt, ctrl and shift---are internally normalised to any of the variants before checking whether a hotkey should be triggered.

The reason for this limitation is that most users would expect to be able to use any modifier key on the keyboard, and pre-normalisation was the simplest solution for this. You can find the implementation of the normalisation here if you would like to contribute an improvement!

wolf0001 commented 3 years ago

I managed to find a solution, thank you to "Nitratine" https://nitratine.net/blog/post/how-to-make-hotkeys-in-python/ I modified his code for my particular hotkeys as follows: combination_to_function = { frozenset([KeyCode(vk=83)]): function_1, # s frozenset([KeyCode(vk=70)]): function_2, # f frozenset([KeyCode(vk=81)]): function_3, # q frozenset([KeyCode(vk=160)]): function_4, # shift_l frozenset([KeyCode(vk=162)]): function_5, # ctrl_l frozenset([KeyCode(vk=164)]): function_6, # alt_l } Thanks for your help on this. Although I am a programmer in other languages, this is my first attempt at python. Wolf

On Wed, Jun 30, 2021 at 11:53 AM moses-palmer @.***> wrote:

Closed #396 https://github.com/moses-palmer/pynput/issues/396.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/moses-palmer/pynput/issues/396#event-4960666254, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUE7D7MR4IKSD5VQ754IMH3TVM4YBANCNFSM47MXL2UA .