使用Pynput键盘检测小键盘按键

时间:2019-10-20 23:16:18

标签: python keyboard hotkeys pynput numpad

我正在使用Pynput键盘模块来检测python应用程序中的击键。

目前我无法区分数字键盘键和常规数字键,它们都返回为“ 1”,“ 2”,“ 3”等

那我想念什么?

代码:

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()

1 个答案:

答案 0 :(得分:1)

您可以使用vk对象的key属性获得virtual key code,对于从数字键盘键输入的数字,其范围是96到105:

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()