Checking a specific key with pynput in Python

前端 未结 3 1671
有刺的猬
有刺的猬 2020-12-20 03:49
dpressed = 0

def on_press(key):

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

Whe

3条回答
  •  有刺的猬
    2020-12-20 04:22

    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)
    

提交回复
热议问题