0

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()
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • how about setting a flag. sort of 1 or 0. if the key is pressed, then you set it's value to 1 while the others are left as 0. – Bernard 'Beta Berlin' Parah Mar 14 '17 at 22:23
  • Did you try the `nodelay` function to allow non blocking key behaviour? You would read the key value and consider it a key repeat until the value returned by `getch` returned to -1. – Paul Rooney Mar 14 '17 at 22:52

0 Answers0