Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. import pyHook
  2. import threading
  3. import win32con
  4. import pythoncom
  5. import time
  6. import pyautogui
  7.  
  8. class WindowsHooksWrapper(object):
  9. """
  10. Provides a means to subscribe to keyboard and mouse events via Windows Hooks
  11.  
  12. It is important to note that:
  13. * A thread specific hook (one that is not injected via dll injection) must be registered on the
  14. same thread with the windows msg pump or it will not work and no indication of error is given
  15. """
  16. def __init__(self):
  17. self.consuming_keyboard_events = False
  18. self.consuming_mouse_events = False
  19. self.hook_manager = None
  20. self.started = False
  21. self.thread = threading.Thread(target=self.thread_proc)
  22.  
  23. def __del__(self):
  24. self.stop()
  25.  
  26. def start(self):
  27. if self.started:
  28. self.stop()
  29.  
  30. self.started = True
  31. self.thread.start()
  32.  
  33. def stop(self):
  34. if not self.started:
  35. return
  36.  
  37. self.started = False
  38. self.thread.join()
  39.  
  40. def consume_mouse_events(self, should_consume_events):
  41. """
  42. Tell the windows hooks wrapper to consume mouse events or not.
  43. Consumed events will not be passed to other hooks or the process they were intended for.
  44. Injected events will be passed on.
  45. :param should_consume_events: set to True to consume mouse events. Otherwise, False
  46. """
  47. if should_consume_events:
  48. print 'Consuming mouse events'
  49. else:
  50. print 'No longer consuming mouse events'
  51.  
  52. self.consuming_mouse_events = should_consume_events
  53.  
  54. def consume_keyboard_events(self, should_consume_events):
  55. """
  56. Tell the windows hooks wrapper to consume keyboard events or not.
  57. Consumed events will not be passed to other hooks or the process they were intended for.
  58. Injected events will be passed on.
  59. :param should_consume_events: set to True to consume keyboard events. Otherwise, False
  60. """
  61. if should_consume_events:
  62. print 'Consuming keyboard events'
  63. else:
  64. print 'No longer consuming keyboard events'
  65. self.consuming_keyboard_events = should_consume_events
  66.  
  67. def on_keyboard_event(self, event):
  68. """
  69. Called back from pyHooks library on a keyboard event
  70. :param event: event passed from pyHooks
  71. :return: True if we are to pass the event on to other hooks and the process it was intended
  72. for. False to consume the event.
  73. """
  74. # Provide a means to stop consuming events while we are consuming all input
  75. if event.KeyID == win32con.VK_ESCAPE:
  76. self.consuming_keyboard_events = False
  77. self.consuming_mouse_events = False
  78. # Consume the event
  79. print 'Escape key hit. Turning input blocking off.'
  80. return False
  81.  
  82. if not self.consuming_keyboard_events or event.Injected:
  83. print 'MessageName:', event.MessageName
  84. print 'Message:', event.Message
  85. print 'Time:', event.Time
  86. print 'Window:', event.Window
  87. print 'WindowName:', event.WindowName
  88. print 'Ascii:', event.Ascii, chr(event.Ascii)
  89. print 'Key:', event.Key
  90. print 'KeyID:', event.KeyID
  91. print 'ScanCode:', event.ScanCode
  92. print 'Extended:', event.Extended
  93. print 'Injected:', event.Injected
  94. print 'Alt', event.Alt
  95. print 'Transition', event.Transition
  96. print '---'
  97. # Send the event to other handlers and its target
  98. return True
  99. else:
  100. # Consume the event. Any other hooks will not receive the event, nor will the process
  101. # the event was intended for.
  102. print 'Consumed keyboard event'
  103. return False
  104.  
  105. def on_mouse_event(self, event):
  106. """
  107. Called back from pyHooks library on a mouse event
  108. :param event: event passed from pyHooks
  109. :return: True if we are to pass the event on to other hooks and the process it was intended
  110. for. False to consume the event.
  111. """
  112. if not self.consuming_mouse_events or event.Injected:
  113. # Send the event to pub sub
  114. print 'MessageName:', event.MessageName
  115. print 'Message:', event.Message
  116. print 'Time:', event.Time
  117. print 'Window:', event.Window
  118. print 'WindowName:', event.WindowName
  119. print 'Position:', event.Position
  120. print 'Wheel:', event.Wheel
  121. print 'Injected:', event.Injected
  122. print '---'
  123. # Send the event to other handlers and its target
  124. return True
  125. else:
  126. # Consume the event. Any other hooks will not receive the event, nor will the process
  127. # the event was intended for.
  128. print 'Consumed mouse event'
  129. return False
  130.  
  131. def thread_proc(self):
  132. print "Thread started"
  133.  
  134. # Evidently, the hook must be registered on the same thread with the windows msg pump or
  135. # it will not work and no indication of error is seen
  136. # Also note that for exception safety, when the hook manager goes out of scope, the
  137. # documentation says that it unregisters all outstanding hooks
  138. self.hook_manager = pyHook.HookManager()
  139. self.hook_manager.KeyAll = self.on_keyboard_event
  140. self.hook_manager.HookKeyboard()
  141. self.hook_manager.MouseAll = self.on_mouse_event
  142. self.hook_manager.HookMouse()
  143.  
  144. while self.started:
  145. pythoncom.PumpWaitingMessages()
  146.  
  147. print "Thread exiting..."
  148.  
  149. self.hook_manager.UnhookKeyboard()
  150. self.hook_manager.UnhookMouse()
  151. self.hook_manager = None
  152.  
  153. def main():
  154. hook_wrapper = WindowsHooksWrapper()
  155. hook_wrapper.start()
  156. hook_wrapper.consume_keyboard_events(True)
  157. hook_wrapper.consume_mouse_events(True)
  158. pyautogui.moveTo(100, 50)
  159. pyautogui.moveTo(200, 200)
  160. time.sleep(30)
  161.  
  162. hook_wrapper.stop()
  163.  
  164.  
  165. if __name__ == "__main__":
  166. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement