0

I'm using pynput keyboard module to detect keystrokes in python app.

At the moment I can't differentiate numpad keys from regular number keys, they all return as "1", "2", "3", etc

So what am I missing?

code :

def on_press(key):
    print key

def on_release(key):
    return

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
LAZ
  • 402
  • 4
  • 15

1 Answers1

5

You can use the vk attribute of the key object to obtain the virtual key code, which ranges from 96 to 105 for numbers entered from the numpad keys:

from pynput import keyboard

def on_press(key):
    if hasattr(key, 'vk') and 96 <= key.vk <= 105:
        print('You entered a number from the numpad: ', key.char)

with keyboard.Listener(on_press = on_press) as listener:
     listener.join()
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 2
    I should have looked on here earlier, but I did figure it out after all by looking at the keycode __dict__. So in my implementation I used keyboard.KeyCode(0x60) for example to see if the keycode is num0 (0x60 is num0). Anyway this answer is correct, thanks a lot. Hopefully it helps a few people since from googling this question going through hundreds of pages I couldn't find an answer before. – LAZ Oct 21 '19 at 01:17