Search code examples
pythonkeyboard

detecting key press python for loop doesn't work


I have a for loop code in python that will detect a keypress

import keyboard
for x in range(100):
   if keyboard.is_pressed('f4):
      print('pressed')

but unfortunately, the code doesn't work with the for a loop.


Solution

  • I'd suggest to use a while loop because it is way more better for detecting the keypress and it loops the code forever until the condition is met.

    while True:
       if keyboard.is_pressed('f4'):
          print('pressed')