Andrei Petrache
Andrei Petrache

Reputation: 1

Python while key pressed function

I recently built an robot that's working with the Raspberry Pi 3 B. I have made a script for it to move forward, backward, left/right with some functions.

import curses
#some more code#
while True:
    char = screen.getch()
    if char == ord('q'):
        break
    elif char == ord('w'):
        goForward()
    elif char == ord('s'):
        goBack()
    elif char == ord('d'):
        goRight()
    elif char == ord('a'):
        goLeft()
    elif char == ord('e'):
         goStop()

When I want to turn left, I press a and it turns forever until I press something else or stop("e"). I want to know if there's a way to rewrite this script so that when I press, for example, d to turn right while d is pressed and then stop when the key is released. Just like toy cars. Like:

while(d.key_pressed == 1):
    goRight()

Upvotes: 0

Views: 845

Answers (0)

Related Questions