def StartApp(): print("Press Num Lock to quit") # hook input winput.hook_mouse(mouse_callback) winput.hook_keyboard(keyboard_callback) # enter message loop winput.wait_messages() # remove input hook winput.unhook_mouse() winput.unhook_keyboard()
def createNewMacro(name=None): global macros, current_macro, last_time, options, start_time, last_flip if not name: name = "Macro #{}".format(len(macros)) start_time = time.time() last_time = 0 last_flip = time.time() winput.hook_mouse(hookAllEvents) winput.hook_keyboard(hookAllEvents) winput.wait_messages() if current_macro: macros[name] = current_macro current_macro = []
def start_logging(game): global start start = time.time() # log start time with open(session_file, "a") as f: json.dump({"input start": start}, f) # set the hook hook_keyboard(on_keyboard_event) hook_mouse(on_mouse_event) start_game(game) update_json(session_file, {"game start": time.time(), "game": game}) start_recording() time.sleep(1) update_json(session_file, {"recording start": get_obs_log_time()}) # set exit handler to log session data print("registering exit handler") atexit.register(exit_handler) # listen for input wait_messages()
def request_mouse_pos(timeout): global last_request_position, last_request_timestamp last_request_position = None last_request_timestamp = time.time() winput.hook_mouse(request_mouse_callback) while True: now = time.time() winput.get_message() if last_request_position and last_request_timestamp + timeout < now: break winput.unhook_mouse() return last_request_position
import winput, time def mouse_callback( event ): if event.action == winput.WM_LBUTTONDOWN: print("Left mouse button press at {}".format( event.position )) def keyboard_callback( event ): if event.vkCode == winput.VK_ESCAPE: # quit on pressing escape winput.stop() print("Press escape to quit") # hook input winput.hook_mouse( mouse_callback ) winput.hook_keyboard( keyboard_callback ) # enter message loop try: while 1: time.sleep(1./120) msg = (winput.get_message()) if msg: break except KeyboardInterrupt: pass # remove input hook winput.unhook_mouse() winput.unhook_keyboard()
def create_macro(name, start_at, screen_width, screen_height): global start, raw_data, stop_recording_key mode = config.get("recording_mode", "key") duration = config.get("recording_duration", 10) stop_recording_key = config.get("recording_stop_key", winput.VK_ESCAPE) mouse_enabled = config.get("record_mouse", True) keyboard_enabled = config.get("record_keyboard", True) assert mode in ("timer", "key") assert type(duration) in (float, int) and duration > 0 assert type(stop_recording_key) == int and 0 <= stop_recording_key <= 2**15 assert type(mouse_enabled) == type(keyboard_enabled) == bool assert mouse_enabled or keyboard_enabled raw_data = [] while True: if time.time() > start_at: break time.sleep(0.0001) start = perf_counter_ns() start_mouse_pos = None if mode == "timer": if mouse_enabled: start_mouse_pos = winput.get_mouse_pos() winput.hook_mouse(callback) if keyboard_enabled: winput.hook_keyboard(callback) while True: now = perf_counter_ns() if now >= start + duration * 1000000000: break winput.get_message() elif mode == "key": if mouse_enabled: start_mouse_pos = winput.get_mouse_pos() winput.hook_mouse(callback) if keyboard_enabled: winput.hook_keyboard(callback_with_stop_key) else: winput.hook_keyboard(callback_only_stop_key) winput.wait_messages() winput.unhook_mouse() winput.unhook_keyboard() return Macro.from_raw_data(name, start, start_mouse_pos, screen_width, screen_height, raw_data)