def record_mouse(duration=5, filename='vpt-mouse.json'): '''Records all mouse events and saves them to a JSON file. Note that it currently blocks the main thread for the given duration.''' events = [] def callback(event): if isinstance(event, mouse.ButtonEvent): events.append({ 'type': 'button', 'button': event.button, 'action': event.event_type, 'time': event.time, }) elif isinstance(event, mouse.WheelEvent): events.append({ 'type': 'wheel', 'delta': event.delta, 'time': event.time, }) elif isinstance(event, mouse.MoveEvent): events.append({ 'type': 'move', 'position': { 'x': event.x, 'y': event.y, }, 'time': event.time, }) mouse.hook(callback) time.sleep(duration) mouse.unhook(callback) with open(filename, 'w') as file: json.dump(events, file, indent=4)
def record_Shortcut(name): #create empty lists for the sequences of mouse and keyboard inputs mouse_events = [] keyboard_events = [] #waits for special keypress before starting the recording print("Press the '*' key when you would like to start recording") keyboard.wait('*') mouse.hook(mouse_events.append) #waits until the same keypress to stop recording keyboard_events = keyboard.record(until='*') mouse.unhook(mouse_events.append) print("Recording done!") #print statements for testing you can remove these #----------------------------------------------- print(str(mouse_events)) print(str(keyboard_events)) #----------------------------------------------- #takes sequence of key and mouse presses as well as the shortcut name and stores in respective files nfile = open("name_shortcuts.txt",'a+') nfile.write("%s\n" % (name)) nfile.close() mfile = open("mouse_shortcuts.txt",'ab+') dump(mouse_events,mfile) mfile.close() kfile = open("keyboard_shortcuts.txt",'ab+') dump(keyboard_events,kfile) kfile.close()
def remove_toggle_mouse(self): """ Remove the mouse toggle key if it exists """ if self.mouse_hotkey != None: mouse.unhook(self.mouse_hotkey) self.mouse_hotkey = None
def test_hook(self): events = [] self.press() mouse.hook(events.append) self.press() mouse.unhook(events.append) self.press() self.assertEqual(len(events), 1)
def _stop_recording(self): keyboard.unhook(self.keyboard_hook) mouse.unhook(self.mouse_hook) # Reset control flags self.keyboard_hook = None self.mouse_hook = None self.recording = False
def _stop(self): controller = self.name.lower() if controller in ["mouse", "fare"]: mouse.unhook(self.add_event) elif controller in ["keyboard", "klavye"]: keyboard.unhook(self.add_event) else: raise ValueError( f"Hatalı değer: {controller} 'mouse' veya 'keyboard' olmalı")
def zaWarudo(): #x1, y1 = win32api.GetCursorPos() recorded = [] mouse.hook(recorded.append) time.sleep(3) mouse.unhook(recorded.append) #print(recorded) print("not funny, didnt happen") mouse.play(reverseMouseEvent(recorded))
def record_loop(dictionary): i = 1 while True: i += 1 keyboard.wait('insert') l = [] print('recording') mouse.hook(l.append) keyboard.wait('print screen') mouse.unhook(l.append) dictionary.update({'{}'.format(str(i)), l})
def record_both(button='esc'): print("recording") keyboard_record = [] mouse_record = [] keyboard.hook(keyboard_record.append) mouse.hook(mouse_record.append) keyboard.wait('esc') keyboard.unhook(keyboard_record.append) mouse.unhook(mouse_record.append) print("done!") return keyboard_record, mouse_record
def record_loop_json(file): i = 1 while True: i += 1 keyboard.wait('insert') l = [] print('recording') mouse.hook(l.append) keyboard.wait('print screen') mouse.unhook(l.append) with open(file, 'a+', encoding='utf/8') as f: json.dump(l)
def getMouseClicks(event): global mUnhooked global mouseClickLocations if keyboard.is_pressed("z"): if mouse.is_pressed(button="left"): mouseClickLocations.append(mouse.get_position()) if mouse.is_pressed(button="middle"): print("unhooking mouse") mouse.unhook(getMouseClicks) mUnhooked = True if kUnhooked and mUnhooked: endOrRestart()
def record_input(): global play_layer recorded = [] mouse.hook(recorded.append) # TODO: add keyboard while True: if play_layer.get_percent() == 0.0: play_layer = game_manager.get_play_layer() if play_layer.percent >= 100 or play_layer.dead: mouse.unhook(recorded.append) newrecorded = [] for r in recorded: if isinstance(r, mouse.ButtonEvent) and r.button == "left": newrecorded.append(r) return newrecorded
def record(start='Ctrl + F3'): macro = Macro() try: print('Waiting for %s to start...' % start) keyboard.wait(start) print('Start recording. ') print('Tip: press \\ for special operations or end recording. ') mouse.hook(macro.appendMouse) while True: op = keyboard.read_event() if op.name == '\\': help = '\\ : \\, t : sleep, esc : stop' print(help, end='\r', flush=True) opUp = passKeyUp(op) superKey = keyboard.read_event() print(' ' * len(help), end='\r') passKeyUp(superKey) if superKey.name == '\\': print('\\') macro.append(Action(getPos(), key=op)) macro.append(Action(getPos(), key=opUp)) elif superKey.name == 't': print('Sleep') macro.append(Sleep(macro.default_interval)) elif superKey.name == 'esc': mouse.unhook(macro.appendMouse) print('End') try: try: while macro[0].key.name in start.lower( ) and macro[0].key.event_type == 'up': macro.pop(0) except AttributeError: pass except IndexError: pass record.last = macro return macro else: print('Invalid super key. ', ' ' * 6) elif 'win' in op.name: print('Sorry, Windows key is reserved for Failsafe. ') else: if op.event_type == 'down': print(op.name) macro.append(Action(getPos(), key=op)) except MismatchError: return None
def OnMouseEvent(event): ### unhook the mouse if mouse.is_pressed(button='middle'): print("unhooking mouse") mouse.unhook(OnMouseEvent) global mouseStatus mouseStatus = True if (mouseStatus and keyboardStatus): MainLoop() if type( event ).__name__ == 'ButtonEvent' and event.event_type == 'down': ### is this a button click? currentTime = event.time previousTime = 0 ### grab the last event time if available if mouseEvents != []: previousTime = mouseEvents[-1][4] button = event.button location = mouse.get_position() eventTime = event.time ### need to also get the currently focused window window = GetWindowText(GetForegroundWindow()) ### this is just to get the name of the window if we click on an out of focus window if window == '' and not 0 <= location[0] <= 45 and not 765 <= location[ 1] <= 729: ### start button on windows ### simulate a button click pyautogui.click(clicks=1) if currentTime - previousTime < 0.40: ### remove the previous event if (currentTime - previousTime < 0.006): ### this must be a simulated click mouseEvents[-1] = ('mouse', button, location, window, eventTime) else: del mouseEvents[-1] mouseEvents.append( ('mouse', 'double', location, window, eventTime)) else: mouseEvents.append(('mouse', button, location, window, eventTime)) return True
def main(): mouse_minute_data = {} keyboard_minute_data = {} mins = int(input('How many minutes would you like to analyze? ')) print() i = 0 while i < mins: mouse_events = [] mouse.hook(mouse_events.append) keyboard.start_recording() time.sleep(60) mouse.unhook(mouse_events.append) keyboard_events = keyboard.stop_recording() mouse_minute_data[i + 1] = len(mouse_events) keyboard_minute_data[i + 1] = len(keyboard_events) i += 1 for (mouse_minute, mouse_data), (keyboard_minute, keyboard_data) in zip(mouse_minute_data.items(), keyboard_minute_data.items()): print('Minute ', mouse_minute, ':') print(mouse_data, 'mouse events') print(keyboard_data, 'keyboard events') print() plt.plot(mouse_minute_data.keys(), mouse_minute_data.values(), color='red', label='Mouse Events') plt.plot(keyboard_minute_data.keys(), keyboard_minute_data.values(), color='blue', label='Keyboard Events') plt.title('Productivity Analysis') plt.xlabel('Minutes') plt.ylabel('Events per minute') plt.legend() plt.show()
def record(self, escape_key="esc"): """ Records all the key press and mouse movements and return a list of recorded events. Pressing the escape_key stops the recording. Returns the recorded events. """ self.stop_replay = False mouse.hook(self.mouse_events_queue.put) keyboard.start_recording(self.keyboard_events_queue) print("Recoding until esc is pressed...") keyboard.wait(escape_key) mouse.unhook(self.mouse_events_queue.put) keyboard.stop_recording() events_queue = self.merge(self.mouse_events_queue, self.keyboard_events_queue) self.events = list(events_queue.queue) print("Recording completed.") return self.events
def handle_mouse(evt, x0=x0, y0=y0): # Cancel tracking if click x_value = DMXSender.raw_state.get(x_channel, 0) y_value = DMXSender.raw_state.get(y_channel, 0) if isinstance(evt, mouse._mouse_event.ButtonEvent): mouse.unhook(handle_mouse) new_x, new_y = mouse.get_position() direction = 'none' shortcut_return = False if not keyboard.is_pressed(42): # if not shift key if new_x > x0: x_value = self.clamp(x_value + 5) direction = 'right' elif new_x < x0: x_value = self.clamp(x_value - 5) direction = 'left' else: shortcut_return = True if not keyboard.is_pressed(29): # if not ctrl key if new_y > y0: y_value = self.clamp(y_value - 5) direction = 'down' elif new_y < y0: y_value = self.clamp(y_value + 5) direction = 'up' elif shortcut_return: return if VERBOSE: print( f'handle_mouse [+]: mouse moved {direction} {new_x=} {x_value=} {new_y=} {y_value=}') mouse.move(x0, y0) fade_arguments = [] print(f'{x_channel=}, {y_channel=}, {x_value=}, {y_value=}') if x_channel is not False: fade_arguments.append([x_channel, x_value, 0, 'linear']) if y_channel is not False: fade_arguments.append([y_channel, y_value, 0, 'linear']) DMXSender.fade_instant(*fade_arguments) # DMXSender.fade(*fade_arguments) # mouse.unhook(handle_mouse) # eel.update_node(node, (x_value, y_value)) return x_value, y_value
def record(): recorded = queue.Queue() m_button_state = checkMouseState( 0x04) # middle button down = 0 or 1. Button up = -127 or -128 macroStart = False print('press middle btn to start recording') while not macroStart: new_m_button_state = checkMouseState(0x04) if new_m_button_state != m_button_state: m_button_state = new_m_button_state if new_m_button_state == '': print('Macro Recording ...') macroStart = True # global mouse 이벤트를 후킹 mo.hook(recorded.put) # global Keyboard 이벤트를 후킹 keyHooked = key.hook(recorded.put) until = False while not until: new_m_button_state = checkMouseState(0x04) if new_m_button_state != m_button_state: m_button_state = new_m_button_state if new_m_button_state == '': print('Macro Recording End') until = True # 후킹 종료 mo.unhook(recorded.put) key.unhook(keyHooked) #return_list = [mo_first_pos] + list(recorded.queue) return_list = list(recorded.queue) return return_list
def record(): width = pyautogui.size()[0] height = pyautogui.size()[1] base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) mouse_events = [] #a mouse.hook(mouse_events.append) keyboard.start_recording() time.sleep(.2) #we move to arbitrary position and press a harmless key so the keyboard and mouse events start recording at the same time. pyautogui.moveTo(width / 2, height / 2) #move mouse to middle of screen pyautogui.press('left') keyboard.wait("`") #press this key to stop recording mouse.unhook(mouse_events.append) keyboard_events = keyboard.stop_recording() print(base_dir) pickle.dump(mouse_events, open(base_dir + r'/events/mouse.p', 'wb')) pickle.dump(keyboard_events, open(base_dir + r'/events/keyboard.p', 'wb'))
def loop(self): """a F10 to start recording F10 again to end recording :return: """ while True: keyboard.wait("ctrl + F10") # Waiting for 'F10' to be pressed self.begin() self.events = [ ] # This is the list where all the events will be stored mouse.hook(self.record_mouse) # starting the mouse recording keyboard.hook(self.record_keyboard) # starting the mouse recording keyboard.wait("ctrl + F10") # Waiting for 'a' to be pressed mouse.unhook(self.record_mouse) # Stopping the mouse recording keyboard.unhook( self.record_keyboard) # starting the mouse recording self.end() # # # a = ActionRecorder() # a.loop()
def unhook(self): mouse.unhook(self.mouse_events.append) keyboard.unhook_all()
import keyboard import pyautogui import pickle import os ### record function ### print("Enter 'r' to begin recording") keyboard.wait('r') ## wait for 'r' to begin recording print(mouse.get_position()) print("Beginning recording...") print("Enter 's' to stop recording") actions = [] mouse.hook(actions.append) keyboard.wait('s') mouse.unhook(actions.append) ## create templates directory if it doesn't exist' try: os.mkdir("Templates/") except FileExistsError: pass fileName = input("Please enter a name for this template.\n") fileName = "Templates/" + fileName + ".dat" ## save template file with open(fileName, 'wb') as f: pickle.dump(actions, f) f.close()
def main(): #global vars path_ini = "202020.ini" path_log = "202020.log" time_work_s = 60 #20*60 time_break_s = 5 #25 time_idle_s = 5 #5*60 icon = "icon.png" messages_break = [ "You have worked for 20 minutes straight. Please look at something 20 feet away for 20 seconds.", "It's that time again to look away from the screen for 20 seconds.", "Do your eyes a favor and look at something 20 feet away.", "To prevent eye strain, please give your eyes a 20 second break.", "Good work! Remember to blink and look away from the computer for a while.", "Remember the 20-20-20 rule.", "Take that well-deserved 20 second break." ] messages_resume = [ "Back to work!", "Thank you for taking care of your eyes.", "You may resume.", "20 seconds has passed. Good job.", "You're doing great! Time to get back to work." ] #setup toaster = ToastNotifier() config = configparser.ConfigParser() logging.basicConfig( filename=path_log, filemode='a', format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%c', level=logging.DEBUG) #start logging.info("202020 Start") timer_work_start = time.time() timer_last_active = time.time() def activityRefresh(e): global timer_last_active timer_last_active = time.time() mouse.hook(activityRefresh) while True: timer_work_while = time.time() #if we have been idle for greater than threshold, keep starting timer in sync with while timer if (timer_work_while - timer_last_active) >= time_idle_s: timer_work_start = time.time() time.sleep(0.1) print(("%s - %s") % (timer_work_while, timer_last_active)) if timer_work_while - timer_work_start >= time_work_s: logging.info("Break Start") toaster.show_toast("202020", random.choice(messages_break), icon_path=icon, duration=10, threaded=True) #read from config every time config.read(path_ini) #if intrusive == True: also block input intrusive = config['DEFAULT']['intrusive'] if ( 'intrusive' in config['DEFAULT']) else False logging.info(("intrusive=%s") % intrusive) if (intrusive): mouse.hook(mouseblock) keyboard.hook(keyboardblock, True) logging.info("Input Blocked.") timer_break_start = time.time() timer_break_while = time.time() while timer_break_while - timer_break_start < time_break_s: timer_break_while = time.time() if (intrusive): mouse.unhook(mouseblock) keyboard.unhook(keyboardblock) logging.info("Input Unblocked.") logging.info("Break End") toaster.show_toast("202020", random.choice(messages_resume), icon_path=icon, duration=20, threaded=True) timer_work_start = time.time()
import time base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) mouse_events = [] # mouse.hook(mouse_events.append) keyboard.start_recording() time.sleep(.2) #we move to arbitrary position and press a harmless key so the keyboard and mouse events start recording at the same time. pyautogui.moveTo(500, 200) pyautogui.press('left') keyboard.wait("a") mouse.unhook(mouse_events.append) keyboard_events = keyboard.stop_recording() #Keyboard threadings: k_thread = threading.Thread(target=lambda: keyboard.play(keyboard_events)) k_thread.start() #Mouse threadings: m_thread = threading.Thread(target=lambda: mouse.play(mouse_events)) m_thread.start() #waiting for both threadings to be completed k_thread.join() m_thread.join()
def stop(self): '''Detach the event listener.''' if self.stopped: return self.stopped = True mouse.unhook(self.emit_event)
def stop_collect(self) -> None: mouse.unhook(self.mouse_selection_callback) self.is_run = False
import pickle import time import json import mouse MoveEvent = mouse.MoveEvent ButtonEvent = mouse.ButtonEvent WheelEvent = mouse.WheelEvent recorded2 = [] mouse.hook(recorded2.append) time.sleep(22) mouse.unhook(recorded2.append) # print(recorded2) # print(obj) d = {} i = 1 for e in recorded2: for f in recorded2: if e.x == f.x and e.y == f.y and recorded2.index(e) != recorded2.index( f): i += 1 # print('e') # print('f') # print(recorded2.index(e)) # print(recorded2.index(f)) d["loop{0}".format( i)] = recorded2[recorded2.index(e):recorded2.index(f) + 1] # print('****************') # print('****************')
def record_action(): currentAction = [] mouse.hook(currentAction.append) keyboard.wait("escape") mouse.unhook(currentAction.append) return currentAction