N
N
nemoigollos2020-05-15 22:14:33
Python
nemoigollos, 2020-05-15 22:14:33

Looping while a key is pressed in python?

It is necessary to implement a loop while the key is held down, and when it is released, do not loop, and so on ad infinitum. Tried it in different ways, for example

import keyboard

while True:
    try:

        if keyboard.is_pressed('n') :
            keyboard.press('space')
    except:
        break

But here the cycle doesn't stop and runs endlessly
import keyboard

while True:
    try:

        if keyboard.is_pressed('space') == True:
            keyboard.press('space')
            keyboard.wait('space')
    except:
        break

It no longer works endlessly, but does not do what I need, does not cycle the key when pressed.
Well, I tried a few more variations, also without success.
If anyone knows how to solve the problem, I will be glad to read

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2020-05-15
@dimonchik2013

shoto like while keyboard.is_pressed('space') == True

K
Kostyan4ik, 2020-05-15
@Kostyan4ik

Try like this:

import keyboard

while True:
    if keyboard.is_pressed('space'):
        pass

instead of pass paste your code

T
Timur Pokrovsky, 2020-05-16
@Makaroshka007

Maybe learn python first? Heard anything about if-elif-else? Do you know what try-except does in your code? Do you know what while is?
Here is the solution to the problem:

while keyboard.is_pressed('space'):
      keyboard.press('space')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question