Python (3.6.1) 按键检测和弹出窗口

3

我有点新手Python,我试着在Windows上打印按下的键并在弹出消息中显示键:

import msvcrt
import ctypes  # An included library with Python install.

def Mbox(title, text, style):
    ctypes.windll.user32.MessageBoxW(0, text, title, style)

while True:
    if msvcrt.kbhit()== True:
        key = msvcrt.getch()
        print(key)  # just to show the result
        Mbox(key, key, 1)

问题如下:

1)按键输出不同,例如:“A”是“b'A'”,为什么?如何将其更改为“A”?(弹出窗口上的输出更奇怪,如:按下1时为1X或按下2时为2*x)

2)While True:会让代码一直运行,并通过这种方式保持检测是否已按下键。

3)是否有适用于WindowsLinux的Python按键检测库?

1个回答

0

好的,我找到了一些答案:

1)msvcrt.getwch() getch() 的宽字符变体,返回 Unicode 值。

阅读更多信息:https://docs.python.org/3.6/library/msvcrt.html

2)我猜是的,如果有人能确认一下,我会很高兴。

3)据我所知,没有这样的库,每个操作系统都有不同的库(如果我错了,请在此留言)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接