Python uses pyhook to monitor the keyboard and implement the function of switching songs

Source: Internet
Author: User
This article mainly introduces python's use of pyhook to monitor the keyboard and implement the function of switching songs. a very cool small program allows you to avoid switching games and directly changing songs during the game, if you need a friend, you can refer to the fact that you sometimes like to play games and listen to music when you are playing dota, but you have to switch out the game when switching the next song, however, switching the music's hot key ctrl + alt + direction keys is useless in the game, which is a good thing. today, I try to use python to implement the next keyboard monitoring switch. the following code is posted:

Import pythoncom, pyHookimport win32gui, win32api, win32con Lcontrol_press = FalseLmenu_press = FalseLeft_press = False def OnKeyboardEvent (event ): global Lcontrol_press # add the global keyword global Lmenu_press when using global variables in the function # otherwise, the 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 ()

Now, you can set your player to play at random and press ctrl + alt + left arrow key to switch the music during the game (ctrl and alt are also on the left)
By the way, the three shortcut keys are not a combination key, which means you need to press ctrl first and then open it. press alt and then press the arrow key to switch the music. the order of these three keys cannot be incorrect.

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.