【问题标题】:Get Key Code of Pressed Key from Python从 Python 中获取按键的键码
【发布时间】:2015-10-31 05:42:46
【问题描述】:

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

我查看了这个Python key binding/capture,但这仅显示您在外壳中按下的字符。我想将此值记录在一个变量中,无需打印它(不是用于 Enter 的 python '\n',而是用于 Enter 键的真正键码(如 13,我认为它在 Windows 上)。

【问题讨论】:

  • 使用 ord() 函数获取 python 键码,以数字表示。
  • @MosheRabaev 虽然对我来说它在 Mac 上不起作用......它说我没有正确的编码或其他东西。另外,如何在 ord() 中使用 unicode int ?
  • @MosheRabaev 我要求 unicode 的原因是因为你不能用 ord() 检查像“Enter”这样的键......你可以吗?
  • 你可以按照你的说法 ord('\r') 给你 13。
  • 您可以使用 w,a,s,d 键分别表示上、左、下、右,空格使用 '\w'。

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


【解决方案1】:

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

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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 2020-12-18
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多