从Python获取按键的密钥代码

时间:2015-08-07 17:29:03

标签: python operating-system stdin key-bindings sys

是否可以让python读取用户按下的键(例如Enter或Escape或箭头键)并将该键的键码记录在变量中?

我查看了这个Python key binding/capture,但这只显示了你在shell中按下的字符。我想将这个值记录在一个变量中,不需要打印它(而不是输入的python'\ n',而不是Enter键的真正的键码(比如13,我认为它在Windows上)。

1 个答案:

答案 0 :(得分:0)

这对我有用。 @MosheRabaev是对的,这只是代码版本。我想你只需找到所有特殊键的字符(比如Enter的'\ n')。不知道箭头键的字符是什么......

import wx
user_input = str(raw_input())

user_input += '\r' #If they press the Enter Key
print user_input

if ord(user_input) == wx.WXK_RETURN:
        print "hey"
print "what"
相关问题