Python uses Pyhook to monitor the keyboard and realize the ability to switch songs _python

Source: Internet
Author: User

I sometimes like playing games while listening to music while playing DotA. But to switch the next song must be cut out of the game, and switch the hot key ctrl+alt+ in the game is no use, good egg pain, today try to use Python to implement keyboard monitoring switch next song, The following code is posted

 import pythoncom, Pyhook import win32gui,win32api,win32con lcontrol_press = False lmenu_ Press = False left_press = False def onkeyboardevent (event): Global lcontrol_press #在函数里面使用全局变量的时候要加上global关键字 Globa L 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 Lmen U_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.HOOKM Anager () hm. KeyDown = Onkeyboardevent hm. Hookkeyboard () pythoncom. Pumpmessages () 

Okay, set your player to a random play can be in the game when you press ctrl+alt+ left ARROW keys can switch music (CTRL and ALT are also left)
by the way, the three shortcuts are not key combinations, meaning you have to hold down CTRL and then let go. When you press ALT, the last button to do is switch the music. The order of these three keys cannot be pressed incorrectly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.