def keyboard_callback(event): global counter3 global counter4 global previousvk if str( event.action ) == '256' and event.vkCode != winput.VK_F1 and event.vkCode != winput.VK_F2 and event.vkCode != winput.VK_SHIFT and event.vkCode != winput.VK_TAB and event.vkCode != winput.VK_MENU and event.vkCode != winput.VK_CONTROL and event.vkCode != winput.VK_CAPITAL: eventvk = str(event.vkCode) print(str(event.vkCode)) now = datetime.now() date_time = now.strftime("%Y-%m-%d %H:%M:%S.%f") SendKeyPressData(str(event.vkCode), '', date_time) if event.vkCode == winput.VK_F1: if counter4 == 0: print('F1 was pressed the application will pause') counter4 = 1 winput.unhook_mouse() winput.unhook_keyboard() input('To continue press the enter key..') #winput.hook_mouse( mouse_callback ) winput.hook_keyboard(keyboard_callback) # enter message loop winput.wait_messages() elif counter4 == 1: counter4 = 0 #to stop keyshare press F2 Key if event.vkCode == winput.VK_F2: # quit on pressing Numlock. make it later in a way that it can be switched back on print('F2 key was pressed. Pythonmation has stopped') winput.stop()
def run(self): global continue_playback, enable_playback_interruption for executor in self.event_executor_list: executor.prepare() continue_playback = True enable_playback_interruption = config.get("enable_stop_playback_key", False) if enable_playback_interruption: self.stop_playback_vk_code = config.get("stop_playback_key", winput.VK_ESCAPE) winput.hook_keyboard(self.stop_playback_callback) start_time = perf_counter_ns() for executor in self.event_executor_list: executor.execute_at_time_offset(start_time) if not continue_playback: break if enable_playback_interruption: winput.unhook_keyboard()
def playMacro(macro): global WAIT_BETWEEN_ACTIONS, last_time, root, IS_RELATIVE, options, screen_res, last_flip, MIN_FPS, STOP_PLAYING IS_RELATIVE = (IS_RELATIVE or options["relative"]) total_time = 0 start_time = time.time() last_time = 0 winput.hook_keyboard(hookEndPlaying) for action in macro: if (STOP_PLAYING): break this_time = time.time() total_time += action[0] if (this_time - last_flip) > 1. / MIN_FPS: # root.update() winput.get_message() last_flip = this_time time_delta = max(total_time - (this_time - start_time), 0) action_type = int(action[1]) if WAIT_BETWEEN_ACTIONS: if time_delta: time.sleep(time_delta) if action_type in (winput.WM_KEYUP, winput.WM_SYSKEYUP, winput.WM_KEYDOWN, winput.WM_SYSKEYDOWN): key = int(action[2]) if action_type == winput.WM_KEYUP: # key up ReleaseKey(key) else: PressKey(key) elif action_type == winput.WM_MOUSEMOVE: desired_position = (int(round(action[2] * screen_res[0], 0)), int(round(action[3] * screen_res[1], 0))) if IS_RELATIVE else (int( action[2]), int(action[3])) user32.SetCursorPos(*desired_position) else: current_mouse_position = getMousePosition() relative_position = ( int(round(action[2] * screen_res[0], 0)) - current_mouse_position[0], int(round(action[3] * screen_res[1], 0)) - current_mouse_position[1]) if IS_RELATIVE else ( int(action[2]) - current_mouse_position[0], int(action[3]) - current_mouse_position[1]) mouse_change = 0x0002 if action_type == winput.WM_LBUTTONDOWN else \ 0x0004 if action_type == winput.WM_LBUTTONUP else \ 0x0020 if action_type == winput.WM_MBUTTONDOWN else \ 0x0040 if action_type == winput.WM_MBUTTONUP else \ 0x0008 if action_type == winput.WM_RBUTTONDOWN else \ 0x0010 MouseEvent(0x0001 + mouse_change, *relative_position)
def choose_stop_recording_key(): global stop_recording_key, root # root.update() winput.hook_keyboard(choose_stop_recording_key_hook) winput.wait_messages() winput.unhook_keyboard()
def choose_stop_recording_key(): global button_choose_stop_recording_key, stop_recording_key, root button_choose_stop_recording_key.config(text="Press key...", state=DISABLED) root.update() winput.hook_keyboard(choose_stop_recording_key_hook) winput.wait_messages() winput.unhook_keyboard()
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_key(timeout): global requested_key requested_key = None start = time.time() winput.hook_keyboard(request_key_callback) while not requested_key: now = time.time() if now >= start + timeout: break winput.get_message() winput.unhook_keyboard() return requested_key
def dialogRunMacro(): global macros, root, STOP_PLAYING STOP_PLAYING = False if listbox.curselection(): canvas_is_recording.delete(ALL) canvas_is_recording.create_polygon(20, 20, 60, 40, 20, 60, fill="blue") root.update() macro = macros[listbox.get(listbox.curselection())] def interrupt(event): global STOP_PLAYING, stop_recording_key if event.vkCode == stop_recording_key: STOP_PLAYING = True winput.hook_keyboard(interrupt) repetitions = options["repetitions"].get() if repetitions == 0: while not STOP_PLAYING: playMacro(macro) else: for i in range(repetitions): if STOP_PLAYING: break playMacro(macro) winput.unhook_keyboard() canvas_is_recording.delete(ALL) canvas_is_recording.create_rectangle(20, 20, 60, 60, fill="#161616") if IS_LOCKED: deleteMacro(True)
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 key(): winput.hook_keyboard( keyboard_callback ) winput.wait_messages() winput.unhook_keyboard()
def createNewMacro(name=None): winput.hook_keyboard(hookAllEvents) winput.wait_messages()
def StartApp(): try: thechoice = GetInput() if thechoice == 2: print('') countdown('5rec') print("Press F1 to pause. Press F2 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() elif thechoice == 1: print( 'Enter the full name of the recording file to replay, or choose one from the list below by typing the number (example Num0)' ) linecounter = 0 with open("replayhistory.txt", "r") as f: for line in f: if line != '': print('Num' + str(linecounter) + ': ' + line[:-1]) linecounter = int(linecounter) linecounter = linecounter + 1 f.close() isshortcut = False recordingname = input('') recordingnum = '-1' historyfilelines = '' if (('Num' in recordingname) or ('num' in recordingname)): recordingnum = recordingname.replace('Num', '') recordingnum = recordingname.replace('num', '') isshortcut = True if recordingnum != '-1': with open("replayhistory.txt", "r") as f: historyfilelines = f.readlines() f.close() recordingname = historyfilelines[int(recordingnum)][:-1] if isshortcut == False: with open("replayhistory.txt", "a") as f: f.write(recordingname + '\n') f.close() print('How many times do you want to replay') nrofreplays = input('') nrofreplays = int(nrofreplays) Replayrecording(recordingname, nrofreplays) ##winput.unhook_mouse() winput.unhook_keyboard() except Exception as e: print('Exception was thrown. ' + str(e)) ##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)