最近在实习的时候发现一件很蛋疼的事情,那就是咱们组的项目由于有后台进程,全部每次运行完之后后台进程都必需要本身手动关闭,每次编译以前忘记关就会有一大堆编译错误,我就想直接弄个能够快捷键直接关闭算了python
class KeyboardEvent(HookEvent): ''' Holds information about a mouse event. @ivar KeyID: Virtual key code @type KeyID: integer @ivar ScanCode: Scan code @type ScanCode: integer @ivar Ascii: 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 = flags def GetKey(self): ''' @return: Name of the virtual keycode @rtype: string '''
return HookConstants.IDToName(self.KeyID) def IsExtended(self): ''' @return: Is this an extended key? @rtype: boolean '''
return self.flags & 0x01
def IsInjected(self): ''' @return: Was this event generated programmatically? @rtype: boolean '''
return self.flags & 0x10
def IsAlt(self): ''' @return: Was the alt key depressed? @rtype: boolean '''
return self.flags & 0x20
def IsTransition(self): ''' @return: Is this a transition from up to down or vice versa? @rtype: boolean '''
return self.flags & 0x80 Key = property(fget=GetKey) Extended = property(fget=IsExtended) Injected = property(fget=IsInjected) Alt = property(fget=IsAlt) Transition = property(fget=IsTransition)
import pythoncom import pyHook import os class KeyboardMgr: m_bZeroKeyPressed = False m_bShiftKeyPressed = False def on_key_pressed(self, event): if str(event.Key) == 'Lshift' or str(event.Key) == 'Rshift' and self.m_bZeroKeyPressed != True: self.m_bShiftKeyPressed = True if event.Alt == 32 and str(event.Key) == '0' and self.m_bShiftKeyPressed == True: os.system('TASKKILL /F /IM abc.exe /T') return True def on_key_up(self, event): if str(event.Key) == 'Lshift' or str(event.Key) == 'Rshift': self.m_bShiftKeyPressed = False elif str(event.Key) == '0': self.m_bZeroKeyPressed = False return True keyMgr = KeyboardMgr() hookMgr = pyHook.HookManager() hookMgr.KeyDown = keyMgr.on_key_pressed hookMgr.KeyUp = keyMgr.on_key_up hookMgr.HookKeyboard() pythoncom.PumpMessages()