自己在玩dota的時候有時候喜歡邊玩游戲邊聽音樂,但是切換下一曲的時候必須得切出游戲,而切換音樂的熱鍵ctrl+alt+方向鍵在游戲的時候沒有用,好事蛋疼,今天試試使用python來實現鍵盤監控切換下一曲,下面貼出代碼
import pythoncom, pyHookimport win32gui,win32api,win32con Lcontrol_press = FalseLmenu_press = FalseLeft_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 Truedef 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 = OnKeyboardEventhm.HookKeyboard()pythoncom.PumpMessages()
好了,把你的播放器設置為隨機播放就可以在游戲的時候按下ctrl+alt+左方向鍵就可以切換音樂啦(ctrl和alt也是左邊的)
順便說明下,那三個快捷鍵不是組合鍵,意思是你要先按下ctrl然后放開,在按下alt,最后按一下做方向鍵就切換音樂了.這三個鍵的順序不能按錯.
新聞熱點
疑難解答