Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking a specific key with pynput in Python

Tags:

python

key

pynput

dpressed = 0

def on_press(key):

    if key == ('d'):
        global dpressed
        dpressed+=1
        logging.info("D: %s" % dpressed)

When I run this code and press d, nothing happens, which I suspect is because the key needs to be called something else when checked. Does someone know what it should be?

like image 873
Mats Avatar asked Dec 18 '22 19:12

Mats


1 Answers

You need to format the key to char format else it won't be equeal to the specific character.

Try

if key.char == ('d'):

Full code being:

dpressed = 0

def on_press(key):

    if key.char == ('d'):
        global dpressed
        dpressed+=1
        logging.info("D: %s" % dpressed)
like image 138
UveGotToFailToSucceed Avatar answered Jan 30 '23 02:01

UveGotToFailToSucceed