moses-palmer / pynput

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

The Numpad numbers does not work in HotKeys. #592

Closed PPJUST closed 4 months ago

PPJUST commented 6 months ago

Description The Numpad numbers does not work in HotKeys.

Platform and pynput version Windows 7;pynput 1.7.6

To Reproduce I tried to use the Numpad to respond to the shortcut, but he didn't work Here is my code:

from pynput import keyboard

def on_activate_h():
    print('hotkey pressed')

with keyboard.GlobalHotKeys({
    '<97>': on_activate_h, }) as h:
    h.join()

I found his method in pynput.keyboard.HotKey.press

def press(self, key):
    if key in self._keys and key not in self._state:
        self._state.add(key)
        if self._state == self._keys:
            self._on_activate()

I added some test statements:

def press(self, key):

    print('pressed: ', key, type(key))
    for i in self._keys:
        print('check: ', i, type(i))
        print('is pressed = key: ',key==i)

    if key in self._keys and key not in self._state:
        self._state.add(key)
        if self._state == self._keys:
            self._on_activate()

The following issues were identified:

pressed <97> <class 'pynput.keyboard._win32.KeyCode'>
check <97> <class 'pynput.keyboard._win32.KeyCode'>
is pressed = key False

Why does <97> ! = <97> I tried converting that KeyCode key to str, which solved the problem:

def press(self, key):

    print('pressed: ', key, type(key))
    for i in self._keys:
        print('check: ', i, type(i))
        print('is str(pressed) = str(key): ',str(key)==str(i))
pressed:  <97> <class 'pynput.keyboard._win32.KeyCode'>
check:  <97> <class 'pynput.keyboard._win32.KeyCode'>
is str(pressed) = str(key):  True

So this code, if modified as follows, can accomplish my purpose, but does this result in an unintended error?

def press(self, key):
keys_str = [str(i) for i in self._keys]

    if str(key) in keys_str and key not in self._state:
        self._state.add(key)
        if [str(i) for i in self._state] == [str(i) for i in self._keys]:
            self._on_activate()

Will you fix it?