I control a remote toy car using python code. Currently code below
def getkey(): fd = sys.stdin.fileno() old = termios.tcgetattr(fd) new = termios.tcgetattr(fd) new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO new[6][TERMIOS.VMIN] = 1 new[6][TERMIOS.VTIME] = 0 termios.tcsetattr(fd, TERMIOS.TCSANOW, new) c = None try: c = os.read(fd, 1) finally: termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old) return c def car(): while True: key = getkey() if key == 's': #Down arrow print "Down" Backward() elif key == 'w': #Up arrow print "Up" forward() elif key == 'a': print "left" Left() elif key == 'd': print "Right" Right() elif key == 'q': #Quit print "That It" break def forward(): GPIO.output(11,True) #Move forward
When I press the 'w' forward () method and the machine moves forward, but does not stop until i exit the program or call GPIO.output (11, Flase) from another method.
Is there any key listener that detects the key unlock of a particular key.
For example, if 'w' pressed, called this method, and if it was released, call another method
Ship code
if w_isPressed() forward() else if w_isReleased() stop()
source share