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 hookEndPlaying(event): global STOP_PLAYING key_vkCode = event.vkCode if event.action == winput.WM_KEYDOWN: if key_vkCode == stop_recording_key: STOP_PLAYING = True winput.unhook_keyboard() winput.stop() return
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 exit_handler(): # stop recording input unhook_mouse() unhook_keyboard() # log end of session update_json( session_file, { "length": time.time() - start, # Length in seconds, preserved for accuracy "end": time.time() }) stop_recording() return 1
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)
def hookAllEvents(event): global speed, dx, dy key_vkCode = event.vkCode if event.action == winput.WM_KEYDOWN: if key_vkCode == stop_recording_key: winput.unhook_mouse() winput.unhook_keyboard() winput.stop() return if key_vkCode == 37: dx = -speed if key_vkCode == 38: dy = -speed if key_vkCode == 39: dx = speed if key_vkCode == 40: dy = speed if key_vkCode == 96: MouseEvent(0x0001 + 0x0002, dx=0, dy=0) if key_vkCode == 97: MouseEvent(0x0001 + 0x0002, dx=0, dy=0) if event.action == winput.WM_KEYUP: if key_vkCode == 37: dx = 0 if key_vkCode == 38: dy = 0 if key_vkCode == 39: dx = 0 if key_vkCode == 40: dy = 00 if key_vkCode == 96: MouseEvent(0x0008 + 0x0010, dx=0, dy=0) if key_vkCode == 97: MouseEvent(0x0001 + 0x0004, dx=0, dy=0) MouseEvent(dx=dx, dy=dy)
def hookAllEvents(event): global current_macro, time_delta, start_time, last_time, RECORD_MOVEMENT, hookManager, options, stop_recording_key, IS_RELATIVE, screen_res, last_flip, MIN_FPS IS_RELATIVE = (IS_RELATIVE or options["relative"]) this_time = time.time() time_delta = ((this_time - start_time) - last_time) last_time = this_time - start_time if (this_time - last_flip) > 1. / MIN_FPS: # root.update() last_flip = this_time if type(event) == winput.MouseEvent: if options["mouse"]: x, y = event.position if IS_RELATIVE: x = round(float(x) / screen_res[0], 5) y = round(float(y) / screen_res[1], 5) if event.action == winput.WM_MOUSEMOVE: if RECORD_MOVEMENT: current_macro.append( (time_delta, winput.WM_MOUSEMOVE, x, y)) else: current_macro.append((time_delta, event.action, x, y)) return 1 else: key_vkCode = event.vkCode if event.action == winput.WM_KEYDOWN: if key_vkCode == stop_recording_key: winput.unhook_mouse() winput.unhook_keyboard() winput.stop() return if options["keyboard"]: current_macro.append((time_delta, event.action, key_vkCode))
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 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)