Io gestisco una macchina giocattolo a distanza utilizzando .Come codice python d'ora il codice è il seguitoPython stampa chiave ed uscita Listener
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's It"
break
def forward():
GPIO.output(11,True) #Move forward
quando premo 'w' in avanti() viene chiamato il metodo e la la macchina avanza ma non si ferma fino a quando non ho il esco dal programma o chiamo GPIO.output (11, Flase) da qualche altro metodo.
C'è un listener chiave che rileva il rilascio chiave di una chiave particolare.
Per esempio, se 'w' pressato chiamato questo metodo e, se rilasciato chiamata di un altro metodo codice
Sudo
if w_isPressed()
forward()
else if w_isReleased()
stop()
Non è possibile farlo senza l'effettivo accesso da tastiera. Il tuo terminale invia solo la pressione di un tasto, quindi non puoi sapere quando è stata rilasciata una chiave. – Blender
Potresti riuscire a farlo usando PyGame. – kindall