简体   繁体   English

Python winsound 按键

[英]Python winsound on key press

I am just learning python and am having difficulty with this piece of code.我刚刚学习 python 并且对这段代码有困难。

def down():
    keyPress = win32api.GetAsyncKeyState(0x06)
    print (keyPress)
    if keyPress < 0:
        winsound.Beep(400,200)
    if keyPress > 0:
        winsound.Beep(200,200)
        winsound.Beep(200,200)

My goal is to have the program play the first (400,200) sound after I press VK_XBUTTON2.我的目标是让程序在我按下 VK_XBUTTON2 后播放第一个 (400,200) 声音。 Then after pressing VK_XBUTTON2 again, I want it to play the two (200,200) sounds.然后再次按下 VK_XBUTTON2 后,我希望它播放两个 (200,200) 声音。

In my program the first beep works, and that makes sense as according to the win32api documentation, pressing the button leads to a result of -32767, however how do I check if the button is pressed again?在我的程序中,第一声哔哔声起作用,根据 win32api 文档,按下按钮会导致结果为 -32767,但是如何检查是否再次按下按钮? As if keyPress > 0: does not seem to be working.好像 keyPress > 0: 似乎不起作用。

According to GetAsyncKeyState ,根据GetAsyncKeyState
when the key is down, GetAsyncKeyState will return 0b1000000000000001 and 0b1000000000000000 next time.当键按下时, GetAsyncKeyState将返回0b10000000000000010b1000000000000000下一次。
when the key is not down but was pressed after the previous call to GetAsyncKeyState , GetAsyncKeyState will return 0b0000000000000001 .当键未按下但在上一次调用GetAsyncKeyState后被按下时, GetAsyncKeyState将返回0b0000000000000001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM