Python:获取按键事件并输出其他内容

标签 python c output stdin getchar

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

谢谢

最佳答案

您可以使用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])

关于Python:获取按键事件并输出其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22749157/

相关文章:

python - 启动 jupyter notebook 时出现 fatal error

python - 使用 Python dbapi 转义 MySQL 保留字

python - bash 脚本中的 pip -r 错误

c - BST 中的这个递归函数如何返回有序的链表?

c - IDEONE 字符串操作的奇怪输出

python - 如何在Python中按字母顺序对字符串中的字母进行排序

c++ - 在 C 和 C++ 编程中使用哪个更好?

c - Fortran/MPI 非持久数据的非阻塞发送

java - 格式化程序 - 显示奇怪的输出

LINUX 获取 screen 内容到文件