Python (3.6.1) 按键检测和弹出窗口
Python (3.6.1) keypress detection and popups
我是 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) python 是否有任何库可以检测 windows 和 Linux 的按键 一共?
好的,我找到了一些答案:
1) msvcrt.getwch()
getch() 的宽字符变体,返回一个 Unicode 值。
阅读更多信息:https://docs.python.org/3.6/library/msvcrt.html
2)我猜是的,如果有人能证实我会很高兴。
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) python 是否有任何库可以检测 windows 和 Linux 的按键 一共?
好的,我找到了一些答案:
1) msvcrt.getwch()
getch() 的宽字符变体,返回一个 Unicode 值。
阅读更多信息:https://docs.python.org/3.6/library/msvcrt.html
2)我猜是的,如果有人能证实我会很高兴。
3) 据我所知,每个操作系统都有不同的库(如果我错了,请在这里留言)。