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

Source: Internet
Author: User

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

When you are playing dota, you sometimes like to play games and listen to music, but you have to switch out of the game when switching the next song, and switching the music's Hot Key ctrl + alt + direction key is useless during the game, this is a good thing. Today, I try to use python to implement a keyboard monitoring switch. Next, I will post the code below.

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.


In the event returned by python using pyHookHookManager (), how does eventTime convert to the datetime format?

I don't think it's a timestamp (or a timestamp in the general sense ).
The event here may be KeyboardEvent or MouseEvent (depending on the hook type ). Both classes are subclasses of HookEvent.
HookEvent has the following members:
Message: Keyboard or mouse event message
Time: Seconds since the epoch when the even current
Window: Window handle of the foreground window at the time of the event
WindowName: Name of the foreground window at the time of the event
Here, the description of the Time member is:
Seconds since the epoch when the even current

Epoch here is very interesting.
Generally, when we use the time module in Python or the time. h In the C standard library, we think epoch is (from the Python2.7 time module document ):
The epoch is the point where the time starts. On January 1st of that year, at 0 hours, the "time since the epoch" is zero. For Unix, the epoch is 1970.
However, epoch here is not. Let's take a look at the following small example adapted from the pyhook Official Website:
#-*-Coding: UTF-8-*-import pythoncom, pyHookimport win32apiimport time def OnKeyboardEvent (event): print event. time # Return the time in seconds since the epoch as a floating point number. # The epoch is the point where the time starts. on January 1st of that year, # at 0 hours, the "time since the epoch" is zero. for Unix, the epoch is 1970. print time. time () # Returns the number of milliseconds since windows started print win32api. getTickCount () print 'messagename: ', event. messageName print 'message: ', event. message print 'time: ', Time. ctime (time. time () print 'window: ', event. window print 'windowname: ', event. windowName print 'ascii: ', event. ascii, chr (event. ascii) print 'key: ', event. key prin ...... remaining full text>

In python, how does one perform keyboard listening? In some documents, "hooks" are used. How do third-party libraries pywin32 and PyHook use high-definition

I found you an ion ..

Oldj.net/article/python-hook/
 

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.