learncodebygaming / pydirectinput

Python mouse and keyboard input automation for Windows using Direct Input.
Other
510 stars 90 forks source link

MEDIA_PLAY_PAUSE Key #18

Open FlorianPachler opened 3 years ago

FlorianPachler commented 3 years ago

Is there a way to use keys like "MEDIA_PLAY_PAUSE"? I tried to implement the scancode of this key (0x122), but somehow only hex values with 2 digits are read.

learncodebygaming commented 3 years ago

PyDirectInput as is doesn't support this, but I suppose you're already customizing the source code? I believe you'll need to set the extended key flag for this input. Look at the keyDown() source code to see an example of how we're already doing this for the arrow keys. It'd be nice to get a generic solution for all the extended keys. I found this page to be the most helpful for figuring out how extended keys work, even though all the example code is C++.

Let us know if you get it working!

FlorianPachler commented 3 years ago

Thanks for your quick response, I´m already working on the source code and I got it to work by doing just as you said. I used the same approach as you did to the arrow keys and I took the virtual key codes from here: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes The weird thing is, that it works for keys like MEDIA_PLAY_PAUSE, MEDIA_PLAY_NEXT, ..., but it doesn´t work for LAUNCH_MAIL, LAUNCH_APP1, LAUNCH_MEDIA_SELECT, ... (I additionally tested it on a friend´s PC, but it doesn´t work for him either. We made sure to have "standard apps" configured (for LAUNCH_MAIL etc.)) Do you know what the problem might be?

learncodebygaming commented 3 years ago

That's awesome you got it working.

I know different keyboards can send different key codes for the same action, especially with these special buttons. I would try writing a bit of code to capture keyboard input to see exactly what virtual key code or scan code your keyboard is generating for those buttons. I think the pynput or winput libraries could help you out with that.

FlorianPachler commented 3 years ago

I actually tried it with a friend´s keyboard who has these "open mail" keys etc., but it didn´t work, although we captured the scancode before. But there is another problem I encountered: When I assign a key (like MEDIA_PLAY_PAUSE) to an unused key (like F13) with pynput and pydirectinput and do too many keystrokes in a short time (via an arduino streamdeck), the program quits. This doesn´t happen with pyautogui. Do you know how I could fix that?

learncodebygaming commented 3 years ago

Do you get any sort of error message when the program crashes?

FlorianPachler commented 3 years ago

Oh sorry, here is the error message I get:

Unhandled exception in listener callback
Traceback (most recent call last):
  File "C:\Users\Florian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput\_util\__init__.py", line 162, in inner
    return f(self, *args, **kwargs)
  File "C:\Users\Florian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput\keyboard\_win32.py", line 283, in _process
    self.on_release(key)
  File "C:\Users\Florian\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pynput\_util\__init__.py", line 78, in inner
    if f(*args) is False:
  File "E:\Eigene Dateien\3D Modell\Streamdeck Flo\Dev\streamdeck_soft\streamdeck\_src\keyRemapper.py", line 137, in on_release
    pressed_vks.remove(vk)  # Remove it from the set of currently pressed keys
KeyError: 127

As you see it has something to do with pynput too, but as I said, if I use pyautogui it works perfectly fine. It seems to me that maybe the keys are removed too fast from my set of currently pressed keys, but I don´t know why it works with pyautogui then.

learncodebygaming commented 3 years ago

Are you creating the keyboard listener just once, or are you creating/destroying the listener in a loop? We're making progress, but it might be fastest if I could see your whole code. Feel free to email it to me if you don't want it to be public here (info@learncodebygaming.com).