Why pynput doesn't detect numeric keyboard presses?

Question:

I’m using python 3.7 on windows 7.

Is there any way to detect number (for example: “1”) key presses in pynput module?

I have tried many other modules and i got nothing except for tkinter
which needs a window but i don’t want that.

from pynput import keyboard  
def on_press(key):
     print(key)
     if str(format(key)) == 'Key.1':
         print('Exiting...')
         return False 
with keyboard.Listener(on_press = on_press) as listener:
     listener.join()

It actually only prints the pressed key and never brakes(doesn’t accept the numeric input).

Asked By: Arash.g

||

Answers:

A quick glance at the doc shows that the num lock key may be undefined on some platforms. I don’t have a windows machine to test this on unfortunately.

num_lock = <Key.f1: 0>

The NumLock key. This may be undefined for some platforms.

Answered By: Charles

Your code is testing for the value "1". This is, your pardon for stating the obvious, what pynput returns if you press the 1 key. You want to respond to the NumLock key.

Instead of

if str(format(key)) == '1':

you can code either of these tests, both of which check for the value you are looking for:

if key == keyboard.Key.num_lock:

if str(format(key)) == "Key.num_lock":

But… There are two kinds of NumLock key.

One is Keyboard NumLock and Clear which has both a USB code (0x53) and a Windows virtual key code (0x90). pynput will respond to pressing this key.

The other kind is Keyboard Locking NumLock which works at the keyboard level: it changes the way your keyboard behaves, and Windows doesn’t get to hear about it. It has a USB code (0x83), but it doesn’t have a Windows virtual key code. If there is no virtual key code for the key, there won’t be a Windows message for pressing it, and pynput reports Windows messages.

So the behaviour is hardware-dependent and it may be that your machine has the second kind of NumLock key. If it does, then nothing you can say to pynput will help. This is unlikely, though, unless you are working with a very restricted laptop keyboard.

Answered By: BoarGules

The code you’ve provided seems to be trying to detect Numeral key ‘1’ rather than ‘Num lock’.

@BoarGules has provided a complete answer. Just to add a bit of clarification:

When you monitor keyboard using pynput, if trying to detect a control key, you should compare it with appropriate pynput.keyboard.Key object. In case of checking for num lock, your code should look like this:

if key == keyboard.Key.num_lock:
    print('exiting')

On the other hand, if you’re looking for an alpha-numeric key, compare it with pynput.keyboard.KeyCode:

if key == keyboard.KeyCode(char = '1'):
    print('exiting')
Answered By: Ali Ganjei

Testing all the returns I realized that there is a key.vk function that returns the code of each key. Number 1 on the numerical keyboard returned the value 97.
So by adding this comparison you can include the numeric keyboard:

if key.char == '1' or key.vk == 97
Answered By: Gabriel Moscarde
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.