2017-11-18 29 views
0

私は、cursesからclickへのあらゆるキー押下を検出するさまざまな方法を見つけました。(msvcrtもそうですが、いつかはLinuxで動くようになっていますが)同じ問題:いずれの矢印キーを押しても、それらの関数はすべてb'\xe0'を返しました。私はcmdとpowershellで同じ結果を試しました。私はWin7 Pro 64bitを実行しています。(Python)Windows上での矢印キーの押下

編集:申し訳ありませんが、私はthisコードを使用してmsvcrt.getch()click.getchar()

+0

https://stackoverflow.com/help/mcveを参照して投稿を編集してください。 – SirKometa

答えて

0

を試してみました私は2つの文字で表される矢印キーのを考え出したので、私は最初の文字を入れている場合、それが読み込まれるようにthis solutionを修正しました2番目(そして3番目はLinux?)に変換して、プラットフォームに依存しない文字列に変換します。

# Find the right getch() and getKey() implementations 
try: 
    # POSIX system: Create and return a getch that manipulates the tty 
    import termios 
    import sys, tty 
    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 

    # Read arrow keys correctly 
    def getKey(): 
     firstChar = getch() 
     if firstChar == '\x1b': 
      return {"[A": "up", "[B": "down", "[C": "right", "[D": "left"}[getch() + getch()] 
     else: 
      return firstChar 

except ImportError: 
    # Non-POSIX: Return msvcrt's (Windows') getch 
    from msvcrt import getch 

    # Read arrow keys correctly 
    def getKey(): 
     firstChar = getch() 
     if firstChar == b'\xe0': 
      return {"H": "up", "P": "down", "M": "right", "K": "left"}[getch()] 
     else: 
      return firstChar 
+0

最初の文字の読み込みが 'b 'である場合にのみ、[' getch'](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch)を呼び出す必要があります'\ x00' 'または' b '\ xe0' '。 – eryksun

+0

@eryksunええ、そうです、これが含まれています。 – xieve

+0

Python 3でUnicode文字列を扱う[代替の実装](https://pastebin.com/0CMbB7EK)です。入力バッファを悪い状態にするのを避けるために常にキーペアを取得します(つまり先頭に "\ x00"または "\ xe0")。結合された1番目と2番目の文字を返すことで、マップされていないキー(例:HOME)をより効果的に処理します。 – eryksun

関連する問題