【问题标题】:Python: get key event and output something elsePython:获取关键事件并输出其他内容
【发布时间】:2014-03-30 20:07:08
【问题描述】:

使用 Python 和 C(或仅 Python)如何在程序运行时从键盘接收每个按下的键并将其他键输出到屏幕?例如,如果用户输入“F”,它会输出“ph”。

谢谢

【问题讨论】:

  • 您要解决什么样的问题?这是输入法、游戏还是其他?

标签: python c output stdin getchar


【解决方案1】:

您可以使用msvcrt.getch() 获取键,然后将其映射到字典中的值:

from msvcrt import getch

chars = {b'f': 'ph'}  # You could easily extend this dictionary.

while True:
    z = getch()
    if z in chars:
        print(chars[z])

【讨论】:

  • 也许你的意思是chars.keys():
  • @Calpratt 不。当您对字典执行in 时,它会自动搜索键。您不需要显式获取密钥。
  • 糟糕。对,你是……p2.7又让我惹上麻烦了
  • 非常感谢!字典中的“b”是什么意思?
  • @user3050910 getch() 发送字节。字符串周围的b cruft 代表字节,仅此而已。
猜你喜欢
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多