Recently in the internship found a very painful thing, that is our group of projects because there is a background process, all the background process after each run must be manually shut down, each time the compilation before forgetting to shut down there will be a lot of compilation errors, I would like to directly get a shortcut key directly close it
The first principle of doing this is to be simple, it is natural to do with Python is the best, we can use Pyhook this package can be very convenient to monitor the function of the keyboard Pyhook need to bind a message handler function, Pyhook will pass a keyboardevent such a class come in
classKeyboardEvent (hookevent):" "holds information about a mouse event. @ivar keyid:virtual Key code @type keyid:integer @ivar scancode:scan code @type scancode:integer @ivar ASC Ii:ascii value, if one exists @type ascii:string" " def __init__(self, msg, Vk_code, Scan_code, ASCII, flags, time, hwnd, window_name):" "Initializes an instances of the class." "hookevent.__init__(self, MSG, time, hwnd, window_name) self. KeyID=Vk_code Self. Scancode=Scan_code Self. Ascii=ASCII Self.flags=FlagsdefGetKey (self):" "@return: Name of the virtual keycode @rtype: String" " returnHookconstants.idtoname (self. KeyID)defisextended (self):" "@return: Is this an extended key? @rtype: Boolean" " returnSelf.flags & 0x01defisinjected (self):" "@return: Is this event generated programmatically? @rtype: Boolean" " returnSelf.flags & 0x10defIsalt (self):" "@return: Was the ALT key depressed? @rtype: Boolean" " returnSelf.flags & 0x20defistransition (self):" "@return: Is this a transition from up to down or vice versa? @rtype: Boolean" " returnSelf.flags & 0x80Key= Property (fget=GetKey) Extended= Property (fget=isextended) Injected= Property (fget=isinjected) Alt= Property (fget=Isalt) Transition= Property (Fget=istransition)
Although I think this bag is a bit unreasonable place is no way to get a combination of keys, can only by oneself make a rule to judge is not a combination of keys, but can use on the lineThe official web site already has a very complete monitoring initialization routine, and we can encapsulate our message processing in a single class:
ImportpythoncomImportPyhookImportOSclasskeyboardmgr:m_bzerokeypressed=False m_bshiftkeypressed=Falsedefon_key_pressed (Self, event):ifSTR (event. Key) = ='LShift' orSTR (event. Key) = ='Rshift' andSelf.m_bzerokeypressed! =True:self.m_bShiftKeyPressed=TrueifEvent. Alt = = 32 andSTR (event. Key) = ='0' andself.m_bshiftkeypressed = =True:os.system ('taskkill/f/im abc.exe/t') returnTruedefon_key_up (Self, event):ifSTR (event. Key) = ='LShift' orSTR (event. Key) = ='Rshift': self.m_bshiftkeypressed=FalseelifSTR (event. Key) = ='0': self.m_bzerokeypressed=FalsereturnTruekeymgr=keyboardmgr () hookmgr=Pyhook.hookmanager () Hookmgr.keydown=keyMgr.on_key_pressedhookMgr.KeyUp=KeyMgr.on_key_uphookMgr.HookKeyboard () pythoncom. Pumpmessages ()
PS: Note pythoncom This package reference, may appear No system module ' pywintypes ' Such errors, this time need to put Lib\site-packages\win32 path under the Pywintypes??. DLL (question mark is your version number) Copy to Lib\site-packages\win32\lib here, if you encounter a similar problem is the same solution kill the process directly with the Windows Taskkill command, so that I can only press the shortcut key before each debug to the background process is all closed
Use Python's Pyhook package for keyboard monitoring