From c91a5f31fb171a02f9efd51c1a7960d7be8323ff Mon Sep 17 00:00:00 2001 From: JE-Chen Date: Mon, 11 Oct 2021 09:06:36 -0700 Subject: [PATCH] add osx record add osx record --- je_auto_control/osx/listener/__init__.py | 1 + .../osx/listener/osx_keyboard_listener.py | 17 ----- je_auto_control/osx/listener/osx_listener.py | 63 +++++++++++++++++++ .../osx/listener/osx_mouse_listener.py | 20 ------ je_auto_control/osx/record/__init__.py | 1 + je_auto_control/osx/record/osx_record.py | 26 ++++++++ .../wrapper/auto_control_record.py | 7 ++- je_auto_control/wrapper/platform_wrapper.py | 4 +- 8 files changed, 100 insertions(+), 39 deletions(-) delete mode 100644 je_auto_control/osx/listener/osx_keyboard_listener.py create mode 100644 je_auto_control/osx/listener/osx_listener.py delete mode 100644 je_auto_control/osx/listener/osx_mouse_listener.py create mode 100644 je_auto_control/osx/record/osx_record.py diff --git a/je_auto_control/osx/listener/__init__.py b/je_auto_control/osx/listener/__init__.py index e69de29..4112ed0 100644 --- a/je_auto_control/osx/listener/__init__.py +++ b/je_auto_control/osx/listener/__init__.py @@ -0,0 +1 @@ +from je_auto_control.osx.listener import * diff --git a/je_auto_control/osx/listener/osx_keyboard_listener.py b/je_auto_control/osx/listener/osx_keyboard_listener.py deleted file mode 100644 index 2733a2b..0000000 --- a/je_auto_control/osx/listener/osx_keyboard_listener.py +++ /dev/null @@ -1,17 +0,0 @@ -from Cocoa import * -import time -from Foundation import * -from PyObjCTools import AppHelper - -class AppDelegate(NSObject): - def applicationDidFinishLaunching_(self, aNotification): - NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSEventMaskKeyDown, handler) - -def handler(event): - print(event) - print(str(hex(event.keyCode()))) - -app = NSApplication.sharedApplication() -delegate = AppDelegate.alloc().init() -NSApp().setDelegate_(delegate) -AppHelper.runEventLoop() \ No newline at end of file diff --git a/je_auto_control/osx/listener/osx_listener.py b/je_auto_control/osx/listener/osx_listener.py new file mode 100644 index 0000000..94f7e13 --- /dev/null +++ b/je_auto_control/osx/listener/osx_listener.py @@ -0,0 +1,63 @@ +import sys + +if sys.platform not in ["darwin"]: + raise Exception("should be only loaded on MacOS") + + +from Cocoa import * +import time +from Foundation import * +from PyObjCTools import AppHelper + +from queue import Queue + + +class RecordQueue(object): + + def __init__(self): + self.record_queue = None + + def reset_queue(self): + self.record_queue = Queue() + + +record_queue_manager = RecordQueue() + +class AppDelegate(NSObject): + def applicationDidFinishLaunching_(self, aNotification): + NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSEventMaskKeyDown, keyboard_handler) + NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSEventMaskLeftMouseDown, mouse_left_handler) + NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSEventMaskRightMouseDown, mouse_right_handler) + + +def mouse_left_handler(event): + loc = NSEvent.mouseLocation() + record_queue_manager.record_queue.put(("mouse_left", loc.x, loc.y)) + + +def mouse_right_handler(event): + loc = NSEvent.mouseLocation() + record_queue_manager.record_queue.put(("mouse_right", loc.x, loc.y)) + + +def keyboard_handler(event): + record_queue_manager.record_queue.put(("keyboard", int(hex(event.keyCode()), 16))) + if int(event.keyCode()) == 98: + AppHelper.stopEventLoop() + + +def osx_record(): + record_queue_manager.reset_queue() + app = NSApplication.sharedApplication() + delegate = AppDelegate.alloc().init() + NSApp().setDelegate_(delegate) + AppHelper.runEventLoop() + + +def osx_stop_record(): + return record_queue_manager.record_queue + + +if __name__ == "__main__": + osx_record() + diff --git a/je_auto_control/osx/listener/osx_mouse_listener.py b/je_auto_control/osx/listener/osx_mouse_listener.py deleted file mode 100644 index 1a71739..0000000 --- a/je_auto_control/osx/listener/osx_mouse_listener.py +++ /dev/null @@ -1,20 +0,0 @@ -from Cocoa import * -import time -from Foundation import * -from PyObjCTools import AppHelper - -class AppDelegate(NSObject): - def applicationDidFinishLaunching_(self, aNotification): - NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSEventMaskLeftMouseDown, left_handler) - NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSEventMaskRightMouseDown, right_handler) - -def left_handler(event): - print(event) - -def right_handler(event): - print(event) - -app = NSApplication.sharedApplication() -delegate = AppDelegate.alloc().init() -NSApp().setDelegate_(delegate) -AppHelper.runEventLoop() \ No newline at end of file diff --git a/je_auto_control/osx/record/__init__.py b/je_auto_control/osx/record/__init__.py index e69de29..dc61a36 100644 --- a/je_auto_control/osx/record/__init__.py +++ b/je_auto_control/osx/record/__init__.py @@ -0,0 +1 @@ +from je_auto_control.osx.record import * diff --git a/je_auto_control/osx/record/osx_record.py b/je_auto_control/osx/record/osx_record.py new file mode 100644 index 0000000..f0e7248 --- /dev/null +++ b/je_auto_control/osx/record/osx_record.py @@ -0,0 +1,26 @@ +import sys + +if sys.platform not in ["darwin"]: + raise Exception("should be only loaded on MacOS") + + +from je_auto_control.osx.listener.osx_listener import osx_record +from je_auto_control.osx.listener.osx_listener import osx_stop_record + +from je_auto_control.utils.je_auto_control_exception.exceptions import AutoControlRecordException + + +class OSXRecorder(object): + + + def record(self): + osx_record() + + + def stop_record(self): + record_queue = osx_stop_record() + if record_queue is None: + raise AutoControlRecordException + return osx_stop_record() + +osx_recorder = OSXRecorder() \ No newline at end of file diff --git a/je_auto_control/wrapper/auto_control_record.py b/je_auto_control/wrapper/auto_control_record.py index 9263a11..78d0762 100644 --- a/je_auto_control/wrapper/auto_control_record.py +++ b/je_auto_control/wrapper/auto_control_record.py @@ -50,10 +50,15 @@ def stop_record(): raise AutoControlRecordException(record_not_found_action_error) - if __name__ == "__main__": record() from time import sleep sleep(10) stop_record() sleep(3) + import sys + if sys.platform in ["darwin"]: + record() + stop_record() + + diff --git a/je_auto_control/wrapper/platform_wrapper.py b/je_auto_control/wrapper/platform_wrapper.py index 7834887..77d69e3 100644 --- a/je_auto_control/wrapper/platform_wrapper.py +++ b/je_auto_control/wrapper/platform_wrapper.py @@ -284,6 +284,7 @@ from je_auto_control.osx.screen import osx_screen from je_auto_control.osx.keyboard import osx_keyboard from je_auto_control.osx.keyboard import osx_keyboard_check + from je_auto_control.osx.record.osx_record import osx_recorder elif sys.platform in ["linux", "linux2"]: from je_auto_control.linux_with_x11.core.utils.x11_linux_vk import x11_linux_key_backspace @@ -863,7 +864,8 @@ keyboard_check = osx_keyboard_check mouse = osx_mouse screen = osx_screen - if None in [keys_table, mouse_table, keyboard_check, keyboard, mouse, screen]: + recorder = osx_recorder + if None in [keys_table, mouse_table, keyboard_check, keyboard, mouse, screen, recorder]: raise AutoControlException("Can't init auto control") elif sys.platform in ["linux", "linux2"]: