Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect a keypress event with PyAutoGUI?

How do I use PyAutoGUI to detect a keypress event? In my research, I could not make an example in this model:

import pyautogui

num = 0
if pyautogui.press('b'): # I know the right thing is not to use the press, but, what do I put in place?
    num = 1
like image 930
Mi Tavares Avatar asked Nov 19 '18 19:11

Mi Tavares


2 Answers

Detecting keystrokes isn't possible in PyAutoGUI. You might want to try the Pynput module instead: https://pypi.org/project/pynput/

like image 162
Al Sweigart Avatar answered Sep 25 '22 00:09

Al Sweigart


The python keyboard library provides the exact function you need:

import keyboard

if keyboard.is_pressed('b'):
    num = 1
like image 43
itsCobra98 Avatar answered Sep 22 '22 00:09

itsCobra98