LoginSignup
21
20

More than 5 years have passed since last update.

Pythonでキー入力待ちをしないキー入力

Posted at

ファンクションキーやカーソルキーなども識別可能。
ShiftキーやControlキーの単独入力は判定できないが、文字キーとの同時押しは識別可能。
termios.ISIGのマスクを有効にすると、Cntl+Cでプログラムが停止せずにキー入力として受け付ける。

ポイント

  • termiosでエコーを無効、カノニカルモード無効に設定
  • fcntlでNONBLOCKモードに設定
getkey.py
import fcntl
import termios
import sys
import os

def getkey():
    fno = sys.stdin.fileno()

    #stdinの端末属性を取得
    attr_old = termios.tcgetattr(fno)

    # stdinのエコー無効、カノニカルモード無効
    attr = termios.tcgetattr(fno)
    attr[3] = attr[3] & ~termios.ECHO & ~termios.ICANON # & ~termios.ISIG
    termios.tcsetattr(fno, termios.TCSADRAIN, attr)

    # stdinをNONBLOCKに設定
    fcntl_old = fcntl.fcntl(fno, fcntl.F_GETFL)
    fcntl.fcntl(fno, fcntl.F_SETFL, fcntl_old | os.O_NONBLOCK)

    chr = 0

    try:
        # キーを取得
        c = sys.stdin.read(1)
        if len(c):
            while len(c):
                chr = (chr << 8) + ord(c)
                c = sys.stdin.read(1)
    finally:
        # stdinを元に戻す
        fcntl.fcntl(fno, fcntl.F_SETFL, fcntl_old)
        termios.tcsetattr(fno, termios.TCSANOW, attr_old)

    return chr

if __name__ == "__main__":
    while 1:
        key = getkey()
        # enterで終了、キー入力があれば表示
        if key == 10:
            break
        elif key:
            print(key)
21
20
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20