Python(3.6.1)按键检测和弹出窗口
我对python有点陌生,我试着在windows上按下时打印按键,并在弹出消息中显示按键:Python(3.6.1)按键检测和弹出窗口,python,popup,keypress,msvcrt,Python,Popup,Keypress,Msvcrt,我对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
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) 是而不是True:
使代码始终运行,从而使其能够检测是否按下了键
3) 有没有python库可以检测到windows和Linux的按键?好的,我找到了一些答案: 1)
msvcrt.getwch()
getch()的宽字符变量,返回Unicode值
更多信息请访问:
2) 我想是的,如果有人能证实,我会很高兴的
3) 我不知道,每个op系统都有diff lib(如果我错了,请在这里留个便条)