自己在玩dota的時候有時候喜歡邊玩游戲邊聽音樂,但是切換下一曲的時候必須得切出游戲,而切換音樂的熱鍵ctrl+alt+方向鍵在游戲的時候沒有用,好事蛋疼,今天試試使用python來實現鍵盤監控切換下一曲,下面貼出代碼
import pythoncom, pyHook
import win32gui,win32api,win32con
Lcontrol_press = False
Lmenu_press = False
Left_press = False
def OnKeyboardEvent(event):
global Lcontrol_press #在函數里面使用全局變量的時候要加上global關鍵字
global Lmenu_press #要不然會出錯
global Left_press
print 'Key:', event.Key
if (event.Key == "Lcontrol"):
Lcontrol_press = True
elif(event.Key == "Lmenu"):
Lmenu_press = True
elif(event.Key == "Left"):
Left_press =True
handel_key()
return True
def handel_key() :
global Lcontrol_press
global Lmenu_press
global Left_press
if(Lcontrol_press and Lmenu_press and Left_press):
win32api.keybd_event( 0xB0,win32con.VK_MEDIA_NEXT_TRACK,0,0)
Lcontrol_press = False
Lmenu_press = False
Left_press = False
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
好了,把你的播放器設置為隨機播放就可以在游戲的時候按下ctrl+alt+左方向鍵就可以切換音樂啦(ctrl和alt也是左邊的)
順便說明下,那三個快捷鍵不是組合鍵,意思是你要先按下ctrl然后放開,在按下alt,最后按一下做方向鍵就切換音樂了.這三個鍵的順序不能按錯.
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

