我正在嘗試製作遊戲,您可以立即將用戶的輸入傳輸到計算機,而不必每次都按enter
。我知道如何做到這一點,但我似乎無法找到箭頭鍵的unicode編號。有沒有unicode,或者我只是會卡住wasd?用於箭頭鍵的unicode?
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
class _Getch:
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
getch = Getch()
我使用getch.impl()
作爲試用或錯誤輸入,如是否有一個關鍵的存在,當調用函數時按下,它返回鍵,並且移動。如果沒有按鍵被按下,它就會繼續。
我正在使用Python 2.7.10
測試的功能在Windows如果您試圖處理鍵盤事件或關鍵狀態,你想要的不是Unicode。鍵盤不能這樣工作。查看文檔,瞭解它用於處理鍵盤輸入的內容。如果您使用的是按字符而不是鍵盤鍵工作的內容,則需要使用其他內容。 – user2357112
「*我知道該怎麼做*」 - 你能告訴我們你是怎麼做到的嗎?一旦我們知道你到目前爲止做了什麼,我們可以描述如何處理方向鍵。 –
您可以使用分別與向左,向上,向右和向下箭頭(←,↑,↓,→)對應的代碼點U + 2190至U + 2193的Unicode箭頭符號。然而你使用'msvcrt'的代碼是不正確的。詳情請參閱@Terry Jan Reedy的回答。 – martineau