Advertisement
johnmahugu

pyhookmanager.py - edited by kesh to work on 64bit win

May 27th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 25.65 KB | None | 0 0
  1. from . import cpyHook
  2.  
  3. def GetKeyState(key_id):
  4.     return cpyHook.cGetKeyState(key_id)
  5.  
  6. class HookConstants:
  7.     '''
  8.    Stores internal windows hook constants including hook types, mappings from virtual
  9.    keycode name to value and value to name, and event type value to name.
  10.    '''
  11.     WH_MIN = -1
  12.     WH_MSGFILTER = -1
  13.     WH_JOURNALRECORD = 0
  14.     WH_JOURNALPLAYBACK = 1
  15.     WH_KEYBOARD = 2
  16.     WH_GETMESSAGE = 3
  17.     WH_CALLWNDPROC = 4
  18.     WH_CBT = 5
  19.     WH_SYSMSGFILTER = 6
  20.     WH_MOUSE = 7
  21.     WH_HARDWARE = 8
  22.     WH_DEBUG = 9
  23.     WH_SHELL = 10
  24.     WH_FOREGROUNDIDLE = 11
  25.     WH_CALLWNDPROCRET = 12
  26.     WH_KEYBOARD_LL = 13
  27.     WH_MOUSE_LL = 14
  28.     WH_MAX = 15
  29.  
  30.     WM_MOUSEFIRST = 0x0200
  31.     WM_MOUSEMOVE = 0x0200
  32.     WM_LBUTTONDOWN = 0x0201
  33.     WM_LBUTTONUP = 0x0202
  34.     WM_LBUTTONDBLCLK = 0x0203
  35.     WM_RBUTTONDOWN =0x0204
  36.     WM_RBUTTONUP = 0x0205
  37.     WM_RBUTTONDBLCLK = 0x0206
  38.     WM_MBUTTONDOWN = 0x0207
  39.     WM_MBUTTONUP = 0x0208
  40.     WM_MBUTTONDBLCLK = 0x0209
  41.     WM_MOUSEWHEEL = 0x020A
  42.     WM_MOUSELAST = 0x020A
  43.  
  44.     WM_KEYFIRST = 0x0100
  45.     WM_KEYDOWN = 0x0100
  46.     WM_KEYUP = 0x0101
  47.     WM_CHAR = 0x0102
  48.     WM_DEADCHAR = 0x0103
  49.     WM_SYSKEYDOWN = 0x0104
  50.     WM_SYSKEYUP = 0x0105
  51.     WM_SYSCHAR = 0x0106
  52.     WM_SYSDEADCHAR = 0x0107
  53.     WM_KEYLAST = 0x0108
  54.  
  55.  
  56.     #VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 -' : 0x39)
  57.     #VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 -' : 0x5A)
  58.  
  59.     #virtual keycode constant names to virtual keycodes numerical id
  60.     vk_to_id = {'VK_LBUTTON' : 0x01, 'VK_RBUTTON' : 0x02, 'VK_CANCEL' : 0x03, 'VK_MBUTTON' : 0x04,
  61.                 'VK_BACK' : 0x08, 'VK_TAB' : 0x09, 'VK_CLEAR' : 0x0C, 'VK_RETURN' : 0x0D, 'VK_SHIFT' : 0x10,
  62.                 'VK_CONTROL' : 0x11, 'VK_MENU' : 0x12, 'VK_PAUSE' : 0x13, 'VK_CAPITAL' : 0x14, 'VK_KANA' : 0x15,
  63.                 'VK_HANGEUL' : 0x15, 'VK_HANGUL' : 0x15, 'VK_JUNJA' : 0x17, 'VK_FINAL' : 0x18, 'VK_HANJA' : 0x19,
  64.                 'VK_KANJI' : 0x19, 'VK_ESCAPE' : 0x1B, 'VK_CONVERT' : 0x1C, 'VK_NONCONVERT' : 0x1D, 'VK_ACCEPT' : 0x1E,
  65.                 'VK_MODECHANGE' : 0x1F, 'VK_SPACE' : 0x20, 'VK_PRIOR' : 0x21, 'VK_NEXT' : 0x22, 'VK_END' : 0x23,
  66.                 'VK_HOME' : 0x24, 'VK_LEFT' : 0x25, 'VK_UP' : 0x26, 'VK_RIGHT' : 0x27, 'VK_DOWN' : 0x28,
  67.                 'VK_SELECT' : 0x29, 'VK_PRINT' : 0x2A, 'VK_EXECUTE' : 0x2B, 'VK_SNAPSHOT' : 0x2C, 'VK_INSERT' : 0x2D,
  68.                 'VK_DELETE' : 0x2E, 'VK_HELP' : 0x2F, 'VK_LWIN' : 0x5B, 'VK_RWIN' : 0x5C, 'VK_APPS' : 0x5D,
  69.                 'VK_NUMPAD0' : 0x60, 'VK_NUMPAD1' : 0x61, 'VK_NUMPAD2' : 0x62, 'VK_NUMPAD3' : 0x63, 'VK_NUMPAD4' : 0x64,
  70.                 'VK_NUMPAD5' : 0x65, 'VK_NUMPAD6' : 0x66, 'VK_NUMPAD7' : 0x67, 'VK_NUMPAD8' : 0x68, 'VK_NUMPAD9' : 0x69,
  71.                 'VK_MULTIPLY' : 0x6A, 'VK_ADD' : 0x6B, 'VK_SEPARATOR' : 0x6C, 'VK_SUBTRACT' : 0x6D, 'VK_DECIMAL' : 0x6E,
  72.                 'VK_DIVIDE' : 0x6F ,'VK_F1' : 0x70, 'VK_F2' : 0x71, 'VK_F3' : 0x72, 'VK_F4' : 0x73, 'VK_F5' : 0x74,
  73.                 'VK_F6' : 0x75, 'VK_F7' : 0x76, 'VK_F8' : 0x77, 'VK_F9' : 0x78, 'VK_F10' : 0x79, 'VK_F11' : 0x7A,
  74.                 'VK_F12' : 0x7B, 'VK_F13' : 0x7C, 'VK_F14' : 0x7D, 'VK_F15' : 0x7E, 'VK_F16' : 0x7F, 'VK_F17' : 0x80,
  75.                 'VK_F18' : 0x81, 'VK_F19' : 0x82, 'VK_F20' : 0x83, 'VK_F21' : 0x84, 'VK_F22' : 0x85, 'VK_F23' : 0x86,
  76.                 'VK_F24' : 0x87, 'VK_NUMLOCK' : 0x90, 'VK_SCROLL' : 0x91, 'VK_LSHIFT' : 0xA0, 'VK_RSHIFT' : 0xA1,
  77.                 'VK_LCONTROL' : 0xA2, 'VK_RCONTROL' : 0xA3, 'VK_LMENU' : 0xA4, 'VK_RMENU' : 0xA5, 'VK_PROCESSKEY' : 0xE5,
  78.                 'VK_ATTN' : 0xF6, 'VK_CRSEL' : 0xF7, 'VK_EXSEL' : 0xF8, 'VK_EREOF' : 0xF9, 'VK_PLAY' : 0xFA,
  79.                 'VK_ZOOM' : 0xFB, 'VK_NONAME' : 0xFC, 'VK_PA1' : 0xFD, 'VK_OEM_CLEAR' : 0xFE, 'VK_BROWSER_BACK' : 0xA6,
  80.                 'VK_BROWSER_FORWARD' : 0xA7, 'VK_BROWSER_REFRESH' : 0xA8, 'VK_BROWSER_STOP' : 0xA9, 'VK_BROWSER_SEARCH' : 0xAA,
  81.                 'VK_BROWSER_FAVORITES' : 0xAB, 'VK_BROWSER_HOME' : 0xAC, 'VK_VOLUME_MUTE' : 0xAD, 'VK_VOLUME_DOWN' : 0xAE,
  82.                 'VK_VOLUME_UP' : 0xAF, 'VK_MEDIA_NEXT_TRACK' : 0xB0, 'VK_MEDIA_PREV_TRACK' : 0xB1, 'VK_MEDIA_STOP' : 0xB2,
  83.                 'VK_MEDIA_PLAY_PAUSE' : 0xB3, 'VK_LAUNCH_MAIL' : 0xB4, 'VK_LAUNCH_MEDIA_SELECT' : 0xB5, 'VK_LAUNCH_APP1' : 0xB6,
  84.                 'VK_LAUNCH_APP2' : 0xB7, 'VK_OEM_1' : 0xBA, 'VK_OEM_PLUS' : 0xBB, 'VK_OEM_COMMA' : 0xBC, 'VK_OEM_MINUS' : 0xBD,
  85.                 'VK_OEM_PERIOD' : 0xBE, 'VK_OEM_2' : 0xBF, 'VK_OEM_3' : 0xC0, 'VK_OEM_4' : 0xDB, 'VK_OEM_5' : 0xDC,
  86.                 'VK_OEM_6' : 0xDD, 'VK_OEM_7' : 0xDE, 'VK_OEM_8' : 0xDF, 'VK_OEM_102' : 0xE2, 'VK_PROCESSKEY' : 0xE5,
  87.                 'VK_PACKET' : 0xE7}
  88.  
  89.     #inverse mapping of keycodes
  90.     id_to_vk = dict([(v,k) for k,v in vk_to_id.items()])
  91.  
  92.     #message constants to message names
  93.     msg_to_name = {WM_MOUSEMOVE : 'mouse move', WM_LBUTTONDOWN : 'mouse left down',
  94.                    WM_LBUTTONUP : 'mouse left up', WM_LBUTTONDBLCLK : 'mouse left double',
  95.                    WM_RBUTTONDOWN : 'mouse right down', WM_RBUTTONUP : 'mouse right up',
  96.                    WM_RBUTTONDBLCLK : 'mouse right double',  WM_MBUTTONDOWN : 'mouse middle down',
  97.                    WM_MBUTTONUP : 'mouse middle up', WM_MBUTTONDBLCLK : 'mouse middle double',
  98.                    WM_MOUSEWHEEL : 'mouse wheel',  WM_KEYDOWN : 'key down',
  99.                    WM_KEYUP : 'key up', WM_CHAR : 'key char', WM_DEADCHAR : 'key dead char',
  100.                    WM_SYSKEYDOWN : 'key sys down', WM_SYSKEYUP : 'key sys up',
  101.                    WM_SYSCHAR : 'key sys char', WM_SYSDEADCHAR : 'key sys dead char'}
  102.  
  103.     def MsgToName(cls, msg):
  104.         '''
  105.        Class method. Converts a message value to message name.
  106.  
  107.        @param msg: Keyboard or mouse event message
  108.        @type msg: integer
  109.        @return: Name of the event
  110.        @rtype: string
  111.        '''
  112.         return HookConstants.msg_to_name.get(msg)
  113.  
  114.     def VKeyToID(cls, vkey):
  115.         '''
  116.        Class method. Converts a virtual keycode name to its value.
  117.  
  118.        @param vkey: Virtual keycode name
  119.        @type vkey: string
  120.        @return: Virtual keycode value
  121.        @rtype: integer
  122.        '''
  123.         return HookConstants.vk_to_id.get(vkey)
  124.  
  125.     def IDToName(cls, code):
  126.         '''
  127.        Class method. Gets the keycode name for the given value.
  128.  
  129.        @param code: Virtual keycode value
  130.        @type code: integer
  131.        @return: Virtual keycode name
  132.        @rtype: string
  133.        '''
  134.         if (code >= 0x30 and code <= 0x39) or (code >= 0x41 and code <= 0x5A):
  135.             text = chr(code)
  136.         else:
  137.             text = HookConstants.id_to_vk.get(code)
  138.             if text is not None:
  139.                 text = text[3:].title()
  140.         return text
  141.  
  142.     MsgToName=classmethod(MsgToName)
  143.     IDToName=classmethod(IDToName)
  144.     VKeyToID=classmethod(VKeyToID)
  145.  
  146. class HookEvent(object):
  147.     '''
  148.    Holds information about a general hook event.
  149.  
  150.    @ivar Message: Keyboard or mouse event message
  151.    @type Message: integer
  152.    @ivar Time: Seconds since the epoch when the even current
  153.    @type Time: integer
  154.    @ivar Window: Window handle of the foreground window at the time of the event
  155.    @type Window: integer
  156.    @ivar WindowName: Name of the foreground window at the time of the event
  157.    @type WindowName: string
  158.    '''
  159.     def __init__(self, msg, time, hwnd, window_name):
  160.         '''Initializes an event instance.'''
  161.         self.Message = msg
  162.         self.Time = time
  163.         self.Window = hwnd
  164.         self.WindowName = window_name
  165.  
  166.     def GetMessageName(self):
  167.         '''
  168.        @return: Name of the event
  169.        @rtype: string
  170.        '''
  171.         return HookConstants.MsgToName(self.Message)
  172.     MessageName = property(fget=GetMessageName)
  173.  
  174. class MouseEvent(HookEvent):
  175.     '''
  176.    Holds information about a mouse event.
  177.  
  178.    @ivar Position: Location of the mouse event on the screen
  179.    @type Position: 2-tuple of integer
  180.    @ivar Wheel: Positive if the wheel scrolls up, negative if down, zero otherwise
  181.    @type Wheel: integer
  182.    @ivar Injected: Was this event generated programmatically?
  183.    @type Injected: boolean
  184.    '''
  185.     def __init__(self, msg, x, y, data, flags, time, hwnd, window_name):
  186.         '''Initializes an instance of the class.'''
  187.         HookEvent.__init__(self, msg, time, hwnd, window_name)
  188.         self.Position = (x,y)
  189.         if data > 0: w = 1
  190.         elif data < 0: w = -1
  191.         else: w = 0
  192.         self.Wheel = w
  193.         self.Injected = flags & 0x01
  194.  
  195. class KeyboardEvent(HookEvent):
  196.     '''
  197.    Holds information about a mouse event.
  198.  
  199.    @ivar KeyID: Virtual key code
  200.    @type KeyID: integer
  201.    @ivar ScanCode: Scan code
  202.    @type ScanCode: integer
  203.    @ivar Ascii: ASCII value, if one exists
  204.    @type Ascii: string
  205.    '''
  206.     def __init__(self, msg, vk_code, scan_code, ascii, flags, time, hwnd, window_name):
  207.         '''Initializes an instances of the class.'''
  208.         HookEvent.__init__(self, msg, time, hwnd, window_name)
  209.         self.KeyID = vk_code
  210.         self.ScanCode = scan_code
  211.         self.Ascii = ascii
  212.         self.flags = flags
  213.  
  214.     def GetKey(self):
  215.         '''
  216.        @return: Name of the virtual keycode
  217.        @rtype: string
  218.        '''
  219.         return HookConstants.IDToName(self.KeyID)
  220.  
  221.     def IsExtended(self):
  222.         '''
  223.        @return: Is this an extended key?
  224.        @rtype: boolean
  225.        '''
  226.         return self.flags & 0x01
  227.  
  228.     def IsInjected(self):
  229.         '''
  230.        @return: Was this event generated programmatically?
  231.        @rtype: boolean
  232.        '''
  233.         return self.flags & 0x10
  234.  
  235.     def IsAlt(self):
  236.         '''
  237.        @return: Was the alt key depressed?
  238.        @rtype: boolean
  239.        '''
  240.         return self.flags & 0x20
  241.  
  242.     def IsTransition(self):
  243.         '''
  244.        @return: Is this a transition from up to down or vice versa?
  245.        @rtype: boolean
  246.        '''
  247.         return self.flags & 0x80
  248.  
  249.     Key = property(fget=GetKey)
  250.     Extended = property(fget=IsExtended)
  251.     Injected = property(fget=IsInjected)
  252.     Alt = property(fget=IsAlt)
  253.     Transition = property(fget=IsTransition)
  254.  
  255. class HookManager(object):
  256.     '''
  257.    Registers and manages callbacks for low level mouse and keyboard events.
  258.  
  259.    @ivar mouse_funcs: Callbacks for mouse events
  260.    @type mouse_funcs: dictionary
  261.    @ivar keyboard_funcs: Callbacks for keyboard events
  262.    @type keyboard_funcs: dictionary
  263.    @ivar mouse_hook: Is a mouse hook set?
  264.    @type mouse_hook: boolean
  265.    @ivar key_hook: Is a keyboard hook set?
  266.    @type key_hook: boolean
  267.    '''
  268.     def __init__(self):
  269.         '''Initializes an instance by setting up an empty set of handlers.'''
  270.         self.mouse_funcs = {}
  271.         self.keyboard_funcs = {}
  272.  
  273.         self.mouse_hook = False
  274.         self.key_hook = False
  275.  
  276.     def __del__(self):
  277.         '''Unhook all registered hooks.'''
  278.         self.UnhookMouse()
  279.         self.UnhookKeyboard()
  280.  
  281.     def HookMouse(self):
  282.         '''Begins watching for mouse events.'''
  283.         cpyHook.cSetHook(HookConstants.WH_MOUSE_LL, self.MouseSwitch)
  284.         self.mouse_hook = True
  285.  
  286.     def HookKeyboard(self):
  287.         '''Begins watching for keyboard events.'''
  288.         cpyHook.cSetHook(HookConstants.WH_KEYBOARD_LL, self.KeyboardSwitch)
  289.         self.keyboard_hook = True
  290.  
  291.     def UnhookMouse(self):
  292.         '''Stops watching for mouse events.'''
  293.         if self.mouse_hook:
  294.             cpyHook.cUnhook(HookConstants.WH_MOUSE_LL)
  295.             self.mouse_hook = False
  296.  
  297.     def UnhookKeyboard(self):
  298.         '''Stops watching for keyboard events.'''
  299.         if self.keyboard_hook:
  300.             cpyHook.cUnhook(HookConstants.WH_KEYBOARD_LL)
  301.             self.keyboard_hook = False
  302.  
  303.     def MouseSwitch(self, msg, x, y, data, flags, time, hwnd, window_name):
  304.         '''
  305.        Passes a mouse event on to the appropriate handler if one is registered.
  306.  
  307.        @param msg: Message value
  308.        @type msg: integer
  309.        @param x: x-coordinate of the mouse event
  310.        @type x: integer
  311.        @param y: y-coordinate of the mouse event
  312.        @type y: integer
  313.        @param data: Data associated with the mouse event (scroll information)
  314.        @type data: integer
  315.        @param flags: Flags associated with the mouse event (injected or not)
  316.        @type flags: integer
  317.        @param time: Seconds since the epoch when the even current
  318.        @type time: integer
  319.        @param hwnd: Window handle of the foreground window at the time of the event
  320.        @type hwnd: integer
  321.        '''
  322.         event = MouseEvent(msg, x, y, data, flags, time, hwnd, window_name)
  323.         # kesh edit #func = self.mouse_funcs.get(msg)
  324.         func = self.mouse_funcs.get(int(str(msg)))
  325.         # kesh edit #func = self.keyboard_funcs.get( int(str(msg)) )
  326.         if func:
  327.             return func(event)
  328.         else:
  329.             return True
  330.  
  331.     def KeyboardSwitch(self, msg, vk_code, scan_code, ascii, flags, time, hwnd, win_name):
  332.         '''
  333.        Passes a keyboard event on to the appropriate handler if one is registered.
  334.  
  335.        @param msg: Message value
  336.        @type msg: integer
  337.        @param vk_code: The virtual keycode of the key
  338.        @type vk_code: integer
  339.        @param scan_code: The scan code of the key
  340.        @type scan_code: integer
  341.        @param ascii: ASCII numeric value for the key if available
  342.        @type ascii: integer
  343.        @param flags: Flags associated with the key event (injected or not, extended key, etc.)
  344.        @type flags: integer
  345.        @param time: Time since the epoch of the key event
  346.        @type time: integer
  347.        @param hwnd: Window handle of the foreground window at the time of the event
  348.        @type hwnd: integer
  349.        '''
  350.         event = KeyboardEvent(msg, vk_code, scan_code, ascii, flags, time, hwnd, win_name)
  351.         func = self.keyboard_funcs.get(msg)
  352.         if func:
  353.             return func(event)
  354.         else:
  355.             return True
  356.  
  357.     def SubscribeMouseMove(self, func):
  358.         '''
  359.        Registers the given function as the callback for this mouse event type. Use the
  360.        MouseMove property as a shortcut.
  361.  
  362.        @param func: Callback function
  363.        @type func: callable
  364.        '''
  365.         if func is None:
  366.             self.disconnect(self.mouse_funcs, HookConstants.WM_MOUSEMOVE)
  367.         else:
  368.             self.connect(self.mouse_funcs, HookConstants.WM_MOUSEMOVE, func)
  369.  
  370.     def SubscribeMouseLeftUp(self, func):
  371.         '''
  372.        Registers the given function as the callback for this mouse event type. Use the
  373.        MouseLeftUp property as a shortcut.
  374.  
  375.        @param func: Callback function
  376.        @type func: callable
  377.        '''
  378.         if func is None:
  379.             self.disconnect(self.mouse_funcs, HookConstants.WM_LBUTTONUP)
  380.         else:
  381.             self.connect(self.mouse_funcs, HookConstants.WM_LBUTTONUP, func)
  382.  
  383.     def SubscribeMouseLeftDown(self, func):
  384.         '''
  385.        Registers the given function as the callback for this mouse event type. Use the
  386.        MouseLeftDown property as a shortcut.
  387.  
  388.        @param func: Callback function
  389.        @type func: callable
  390.        '''
  391.         if func is None:
  392.             self.disconnect(self.mouse_funcs, HookConstants.WM_LBUTTONDOWN)
  393.         else:
  394.             self.connect(self.mouse_funcs, HookConstants.WM_LBUTTONDOWN, func)
  395.  
  396.     def SubscribeMouseLeftDbl(self, func):
  397.         '''
  398.        Registers the given function as the callback for this mouse event type. Use the
  399.        MouseLeftDbl property as a shortcut.
  400.  
  401.        @param func: Callback function
  402.        @type func: callable
  403.        '''
  404.         if func is None:
  405.             self.disconnect(self.mouse_funcs, HookConstants.WM_LBUTTONDBLCLK)
  406.         else:
  407.             self.connect(self.mouse_funcs, HookConstants.WM_LBUTTONDBLCLK, func)
  408.  
  409.     def SubscribeMouseRightUp(self, func):
  410.         '''
  411.        Registers the given function as the callback for this mouse event type. Use the
  412.        MouseRightUp property as a shortcut.
  413.  
  414.        @param func: Callback function
  415.        @type func: callable
  416.        '''
  417.         if func is None:
  418.             self.disconnect(self.mouse_funcs, HookConstants.WM_RBUTTONUP)
  419.         else:
  420.             self.connect(self.mouse_funcs, HookConstants.WM_RBUTTONUP, func)
  421.  
  422.     def SubscribeMouseRightDown(self, func):
  423.         '''
  424.        Registers the given function as the callback for this mouse event type. Use the
  425.        MouseRightDown property as a shortcut.
  426.  
  427.        @param func: Callback function
  428.        @type func: callable
  429.        '''
  430.         if func is None:
  431.             self.disconnect(self.mouse_funcs, HookConstants.WM_RBUTTONDOWN)
  432.         else:
  433.             self.connect(self.mouse_funcs, HookConstants.WM_RBUTTONDOWN, func)
  434.  
  435.     def SubscribeMouseRightDbl(self, func):
  436.         '''
  437.        Registers the given function as the callback for this mouse event type. Use the
  438.        MouseRightDbl property as a shortcut.
  439.  
  440.        @param func: Callback function
  441.        @type func: callable
  442.        '''
  443.         if func is None:
  444.             self.disconnect(self.mouse_funcs, HookConstants.WM_RBUTTONDBLCLK)
  445.         else:
  446.             self.connect(self.mouse_funcs, HookConstants.WM_RBUTTONDBLCLK, func)
  447.  
  448.     def SubscribeMouseMiddleUp(self, func):
  449.         '''
  450.        Registers the given function as the callback for this mouse event type. Use the
  451.        MouseMiddleUp property as a shortcut.
  452.  
  453.        @param func: Callback function
  454.        @type func: callable
  455.        '''
  456.         if func is None:
  457.             self.disconnect(self.mouse_funcs, HookConstants.WM_MBUTTONUP)
  458.         else:
  459.             self.connect(self.mouse_funcs, HookConstants.WM_MBUTTONUP, func)
  460.  
  461.     def SubscribeMouseMiddleDown(self, func):
  462.         '''
  463.        Registers the given function as the callback for this mouse event type. Use the
  464.        MouseMiddleDown property as a shortcut.
  465.  
  466.        @param func: Callback function
  467.        @type func: callable
  468.        '''
  469.         if func is None:
  470.             self.disconnect(self.mouse_funcs, HookConstants.WM_MBUTTONDOWN)
  471.         else:
  472.             self.connect(self.mouse_funcs, HookConstants.WM_MBUTTONDOWN, func)
  473.  
  474.     def SubscribeMouseMiddleDbl(self, func):
  475.         '''
  476.        Registers the given function as the callback for this mouse event type. Use the
  477.        MouseMiddleDbl property as a shortcut.
  478.  
  479.        @param func: Callback function
  480.        @type func: callable
  481.        '''
  482.         if func is None:
  483.             self.disconnect(self.mouse_funcs, HookConstants.WM_MBUTTONDBLCLK)
  484.         else:
  485.             self.connect(self.mouse_funcs, HookConstants.WM_MBUTTONDBLCLK, func)
  486.  
  487.     def SubscribeMouseWheel(self, func):
  488.         '''
  489.        Registers the given function as the callback for this mouse event type. Use the
  490.        MouseWheel property as a shortcut.
  491.  
  492.        @param func: Callback function
  493.        @type func: callable
  494.        '''
  495.         if func is None:
  496.             self.disconnect(self.mouse_funcs, HookConstants.WM_MOUSEWHEEL)
  497.         else:
  498.             self.connect(self.mouse_funcs, HookConstants.WM_MOUSEWHEEL, func)
  499.  
  500.     def SubscribeMouseAll(self, func):
  501.         '''
  502.        Registers the given function as the callback for all mouse events. Use the
  503.        MouseAll property as a shortcut.
  504.  
  505.        @param func: Callback function
  506.        @type func: callable
  507.        '''
  508.         self.SubscribeMouseMove(func)
  509.         self.SubscribeMouseWheel(func)
  510.         self.SubscribeMouseAllButtons(func)
  511.  
  512.     def SubscribeMouseAllButtons(self, func):
  513.         '''
  514.        Registers the given function as the callback for all mouse button events. Use the
  515.        MouseButtonAll property as a shortcut.
  516.  
  517.        @param func: Callback function
  518.        @type func: callable
  519.        '''
  520.         self.SubscribeMouseAllButtonsDown(func)
  521.         self. SubscribeMouseAllButtonsUp(func)
  522.         self.SubscribeMouseAllButtonsDbl(func)
  523.  
  524.     def SubscribeMouseAllButtonsDown(self, func):
  525.         '''
  526.        Registers the given function as the callback for all mouse button down events.
  527.        Use the MouseAllButtonsDown property as a shortcut.
  528.  
  529.        @param func: Callback function
  530.        @type func: callable
  531.        '''
  532.         self.SubscribeMouseLeftDown(func)
  533.         self.SubscribeMouseRightDown(func)
  534.         self.SubscribeMouseMiddleDown(func)
  535.  
  536.     def SubscribeMouseAllButtonsUp(self, func):
  537.         '''
  538.        Registers the given function as the callback for all mouse button up events.
  539.        Use the MouseAllButtonsUp property as a shortcut.
  540.  
  541.        @param func: Callback function
  542.        @type func: callable
  543.        '''
  544.         self.SubscribeMouseLeftUp(func)
  545.         self.SubscribeMouseRightUp(func)
  546.         self.SubscribeMouseMiddleUp(func)
  547.  
  548.     def SubscribeMouseAllButtonsDbl(self, func):
  549.         '''
  550.        Registers the given function as the callback for all mouse button double click
  551.        events. Use the MouseAllButtonsDbl property as a shortcut.
  552.  
  553.        @param func: Callback function
  554.        @type func: callable
  555.        '''
  556.         self.SubscribeMouseLeftDbl(func)
  557.         self.SubscribeMouseRightDbl(func)
  558.         self.SubscribeMouseMiddleDbl(func)
  559.  
  560.     def SubscribeKeyDown(self, func):
  561.         '''
  562.        Registers the given function as the callback for this keyboard event type.
  563.        Use the KeyDown property as a shortcut.
  564.  
  565.        @param func: Callback function
  566.        @type func: callable
  567.        '''
  568.         if func is None:
  569.             self.disconnect(self.keyboard_funcs, HookConstants.WM_KEYDOWN)
  570.             self.disconnect(self.keyboard_funcs, HookConstants.WM_SYSKEYDOWN)
  571.         else:
  572.             self.connect(self.keyboard_funcs, HookConstants.WM_KEYDOWN, func)
  573.             self.connect(self.keyboard_funcs, HookConstants.WM_SYSKEYDOWN, func)
  574.  
  575.     def SubscribeKeyUp(self, func):
  576.         '''
  577.        Registers the given function as the callback for this keyboard event type.
  578.        Use the KeyUp property as a shortcut.
  579.  
  580.        @param func: Callback function
  581.        @type func: callable
  582.        '''
  583.         if func is None:
  584.             self.disconnect(self.keyboard_funcs, HookConstants.WM_KEYUP)
  585.             self.disconnect(self.keyboard_funcs, HookConstants.WM_SYSKEYUP)
  586.         else:
  587.             self.connect(self.keyboard_funcs, HookConstants.WM_KEYUP, func)
  588.             self.connect(self.keyboard_funcs, HookConstants.WM_SYSKEYUP, func)
  589.  
  590.     def SubscribeKeyChar(self, func):
  591.         '''
  592.        Registers the given function as the callback for this keyboard event type.
  593.        Use the KeyChar property as a shortcut.
  594.  
  595.        B{Note}: this is currently non-functional, no WM_*CHAR messages are
  596.        processed by the keyboard hook.
  597.  
  598.        @param func: Callback function
  599.        @type func: callable
  600.        '''
  601.         if func is None:
  602.             self.disconnect(self.keyboard_funcs, HookConstants.WM_CHAR)
  603.             self.disconnect(self.keyboard_funcs, HookConstants.WM_DEADCHAR)
  604.             self.disconnect(self.keyboard_funcs, HookConstants.WM_SYSCHAR)
  605.             self.disconnect(self.keyboard_funcs, HookConstants.WM_SYSDEADCHAR)
  606.         else:
  607.             self.connect(self.keyboard_funcs, HookConstants.WM_CHAR, func)
  608.             self.connect(self.keyboard_funcs, HookConstants.WM_DEADCHAR, func)
  609.             self.connect(self.keyboard_funcs, HookConstants.WM_SYSCHAR, func)
  610.             self.connect(self.keyboard_funcs, HookConstants.WM_SYSDEADCHAR, func)
  611.  
  612.     def SubscribeKeyAll(self, func):
  613.         '''
  614.        Registers the given function as the callback for all keyboard events.
  615.        Use the KeyAll property as a shortcut.
  616.  
  617.        @param func: Callback function
  618.        @type func: callable
  619.        '''
  620.         self.SubscribeKeyDown(func)
  621.         self.SubscribeKeyUp(func)
  622.         self.SubscribeKeyChar(func)
  623.  
  624.     MouseAll = property(fset=SubscribeMouseAll)
  625.     MouseAllButtons = property(fset=SubscribeMouseAllButtons)
  626.     MouseAllButtonsUp = property(fset=SubscribeMouseAllButtonsUp)
  627.     MouseAllButtonsDown = property(fset=SubscribeMouseAllButtonsDown)
  628.     MouseAllButtonsDbl = property(fset=SubscribeMouseAllButtonsDbl)
  629.  
  630.     MouseWheel = property(fset=SubscribeMouseWheel)
  631.     MouseMove = property(fset=SubscribeMouseMove)
  632.     MouseLeftUp = property(fset=SubscribeMouseLeftUp)
  633.     MouseLeftDown = property(fset=SubscribeMouseLeftDown)
  634.     MouseLeftDbl = property(fset=SubscribeMouseLeftDbl)
  635.     MouseRightUp = property(fset=SubscribeMouseRightUp)
  636.     MouseRightDown = property(fset=SubscribeMouseRightDown)
  637.     MouseRightDbl = property(fset=SubscribeMouseRightDbl)
  638.     MouseMiddleUp = property(fset=SubscribeMouseMiddleUp)
  639.     MouseMiddleDown = property(fset=SubscribeMouseMiddleDown)
  640.     MouseMiddleDbl = property(fset=SubscribeMouseMiddleDbl)
  641.  
  642.     KeyUp = property(fset=SubscribeKeyUp)
  643.     KeyDown = property(fset=SubscribeKeyDown)
  644.     KeyChar = property(fset=SubscribeKeyChar)
  645.     KeyAll = property(fset=SubscribeKeyAll)
  646.  
  647.     def connect(self, switch, id, func):
  648.         '''
  649.        Registers a callback to the given function for the event with the given ID in the
  650.        provided dictionary. Internal use only.
  651.  
  652.        @param switch: Collection of callbacks
  653.        @type switch: dictionary
  654.        @param id: Event type
  655.        @type id: integer
  656.        @param func: Callback function
  657.        @type func: callable
  658.        '''
  659.         switch[id] = func
  660.  
  661.     def disconnect(self, switch, id):
  662.         '''
  663.        Unregisters a callback for the event with the given ID in the provided dictionary.
  664.        Internal use only.
  665.  
  666.        @param switch: Collection of callbacks
  667.        @type switch: dictionary
  668.        @param id: Event type
  669.        @type id: integer
  670.        '''
  671.         try:
  672.             del switch[id]
  673.         except:
  674.             pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement