[Linux-Biella] su raspberry keypress in python
Leonardo
leonardo.buffa a bilug.it
Mar 22 Gen 2019 11:23:01 CET
On 17/01/19 16:29, Leonardo wrote:
>
> ciao
>
> avrei necessita' di "sentire" quando, mentre sono in ssh, viene
> premuto un tasto (e ovviamente riconoscerlo)
risolto cosi':
import sys
import tty, termios
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
richiamata normalmente in un ciclo
KeyPressed=getch()
attenzione che cosi' non si esce con un ctrl-c ma al massimo un ctrl-z
con un successivo kill del PID, conviene quindi eventualmente trovare
una escapekey e fare:
if KeyPressed == "q": sys.exit()
Maggiori informazioni sulla lista
Linux