Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyHook
- import threading
- import win32con
- import pythoncom
- import time
- import pyautogui
- class WindowsHooksWrapper(object):
- """
- Provides a means to subscribe to keyboard and mouse events via Windows Hooks
- It is important to note that:
- * A thread specific hook (one that is not injected via dll injection) must be registered on the
- same thread with the windows msg pump or it will not work and no indication of error is given
- """
- def __init__(self):
- self.consuming_keyboard_events = False
- self.consuming_mouse_events = False
- self.hook_manager = None
- self.started = False
- self.thread = threading.Thread(target=self.thread_proc)
- def __del__(self):
- self.stop()
- def start(self):
- if self.started:
- self.stop()
- self.started = True
- self.thread.start()
- def stop(self):
- if not self.started:
- return
- self.started = False
- self.thread.join()
- def consume_mouse_events(self, should_consume_events):
- """
- Tell the windows hooks wrapper to consume mouse events or not.
- Consumed events will not be passed to other hooks or the process they were intended for.
- Injected events will be passed on.
- :param should_consume_events: set to True to consume mouse events. Otherwise, False
- """
- if should_consume_events:
- print 'Consuming mouse events'
- else:
- print 'No longer consuming mouse events'
- self.consuming_mouse_events = should_consume_events
- def consume_keyboard_events(self, should_consume_events):
- """
- Tell the windows hooks wrapper to consume keyboard events or not.
- Consumed events will not be passed to other hooks or the process they were intended for.
- Injected events will be passed on.
- :param should_consume_events: set to True to consume keyboard events. Otherwise, False
- """
- if should_consume_events:
- print 'Consuming keyboard events'
- else:
- print 'No longer consuming keyboard events'
- self.consuming_keyboard_events = should_consume_events
- def on_keyboard_event(self, event):
- """
- Called back from pyHooks library on a keyboard event
- :param event: event passed from pyHooks
- :return: True if we are to pass the event on to other hooks and the process it was intended
- for. False to consume the event.
- """
- # Provide a means to stop consuming events while we are consuming all input
- if event.KeyID == win32con.VK_ESCAPE:
- self.consuming_keyboard_events = False
- self.consuming_mouse_events = False
- # Consume the event
- print 'Escape key hit. Turning input blocking off.'
- return False
- if not self.consuming_keyboard_events or event.Injected:
- print 'MessageName:', event.MessageName
- print 'Message:', event.Message
- print 'Time:', event.Time
- print 'Window:', event.Window
- print 'WindowName:', event.WindowName
- print 'Ascii:', event.Ascii, chr(event.Ascii)
- print 'Key:', event.Key
- print 'KeyID:', event.KeyID
- print 'ScanCode:', event.ScanCode
- print 'Extended:', event.Extended
- print 'Injected:', event.Injected
- print 'Alt', event.Alt
- print 'Transition', event.Transition
- print '---'
- # Send the event to other handlers and its target
- return True
- else:
- # Consume the event. Any other hooks will not receive the event, nor will the process
- # the event was intended for.
- print 'Consumed keyboard event'
- return False
- def on_mouse_event(self, event):
- """
- Called back from pyHooks library on a mouse event
- :param event: event passed from pyHooks
- :return: True if we are to pass the event on to other hooks and the process it was intended
- for. False to consume the event.
- """
- if not self.consuming_mouse_events or event.Injected:
- # Send the event to pub sub
- print 'MessageName:', event.MessageName
- print 'Message:', event.Message
- print 'Time:', event.Time
- print 'Window:', event.Window
- print 'WindowName:', event.WindowName
- print 'Position:', event.Position
- print 'Wheel:', event.Wheel
- print 'Injected:', event.Injected
- print '---'
- # Send the event to other handlers and its target
- return True
- else:
- # Consume the event. Any other hooks will not receive the event, nor will the process
- # the event was intended for.
- print 'Consumed mouse event'
- return False
- def thread_proc(self):
- print "Thread started"
- # Evidently, the hook must be registered on the same thread with the windows msg pump or
- # it will not work and no indication of error is seen
- # Also note that for exception safety, when the hook manager goes out of scope, the
- # documentation says that it unregisters all outstanding hooks
- self.hook_manager = pyHook.HookManager()
- self.hook_manager.KeyAll = self.on_keyboard_event
- self.hook_manager.HookKeyboard()
- self.hook_manager.MouseAll = self.on_mouse_event
- self.hook_manager.HookMouse()
- while self.started:
- pythoncom.PumpWaitingMessages()
- print "Thread exiting..."
- self.hook_manager.UnhookKeyboard()
- self.hook_manager.UnhookMouse()
- self.hook_manager = None
- def main():
- hook_wrapper = WindowsHooksWrapper()
- hook_wrapper.start()
- hook_wrapper.consume_keyboard_events(True)
- hook_wrapper.consume_mouse_events(True)
- pyautogui.moveTo(100, 50)
- pyautogui.moveTo(200, 200)
- time.sleep(30)
- hook_wrapper.stop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement