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 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_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
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 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)