Capture device ID on key press in python on windows

Viewed 501

So I've been working on a small project which will essentially be a macros program. The idea is to use a small external numpad with custom labels to click certain areas of the screen which correspond to buttons. I've got this all working beautifully with one minor (or major?) caveat which is the fact that I cannot differentiate between keyboards. Ideally I'd like to be able to still use my main keyboard and numpad normally. My code below for a sample key:

import keyboard
import pynput
from pynput.mouse import Button, Controller
import time
mouse = Controller()

tab = (1639, 16)

def handler():
    print(keyboard.KeyboardEvent.device)
    prev = mouse.position
    mouse.position = tab
    mouse.click(Button.left, 1)
    mouse.position = prev

keyboard.add_hotkey("a", handler, suppress=True)
while True:
    time.sleep(0.1)

So you can see here I am using the keyboard and pynput libraries. In the docs for the keyboard library, it notes an issue where on windows specifically, keyboard.KeyboardEvent.device returns "None" always.

I looked around a bit at various solutions for using raw input, but I wasn't able to really find anything applicable to my situation, and anything that looked remotely close was admittedly beyond my skill level. Anybody have any thoughts? Thanks.

0 Answers
Related