Detect keystrokes in python (tty)

I did the same thing with termios last time, but I also tried it with tty. [Last time] http://qiita.com/tortuepin/items/9ede6ca603ddc74f91ba

import sys
import termios
import tty

#Get standard input file descriptor
fd = sys.stdin.fileno()

#Get the terminal attributes of fd
old = termios.tcgetattr(fd)

try:
    #Switch standard input mode
    #Both cbreak and raw don't need enter, but raw is ctrl-c can't be heard??
    tty.setcbreak(sys.stdin.fileno())
    #tty.setraw(sys.stdin.fileno())
    ch = sys.stdin.read(1)
    
finally:
    #Restore the attributes of fd
    termios.tcsetattr(fd, termios.TCSANOW, old)

print(ch)

This one has less freedom than last time, but it's easier.

Difference between cbreak and raw

https://utcc.utoronto.ca/~cks/space/blog/unix/CBreakAndRaw

Recommended Posts

Detect keystrokes in python (tty)
Detect keystrokes in Python (without Enter)
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Get keystrokes during background execution in Python (windows)
Detect golden crosses on stock charts in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
Read DXF in python
Daily AtCoder # 53 in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python