def move_mouse_relative(m): direction_type = m._words[1].word multiplier = 100 pixels_to_travel = parse_words_as_integer(m._words[2:]) * multiplier if pixels_to_travel == None: return direction_vector = { 'up': (0, -1), 'right': (1, 0), 'down': (0, 1), 'left': (-1, 0) }[direction_type] (x, y) = ctrl.mouse_pos() ctrl.mouse_move(x + direction_vector[0] * pixels_to_travel, y + direction_vector[1] * pixels_to_travel)
def enable(self, persisted=False): if not self.enabled: self.enabled = True self.previous_pos = ctrl.mouse_pos() # Copied over from base widget and altered to reflect the no-canvas state of this widget self.canvas = None if persisted: self.preferences.enabled = True self.preferences.mark_changed = True self.event_dispatch.request_persist_preferences() self.cleared = False self.soft_enable() self.create_canvases()
def gaze_scroll(): #print("gaze_scroll") if eye_zoom_mouse.zoom_mouse.state == eye_zoom_mouse.STATE_IDLE: windows = ui.windows() window = None x, y = ctrl.mouse_pos() for w in windows: if w.rect.contains(x, y): window = w.rect break if window is None: #print("no window found!") return midpoint = window.y + window.height / 2 amount = int(((y - midpoint) / (window.height / 10))**3) actions.mouse_scroll(by_lines=False, y=amount)
def parse_cardinal(direction: str, distance: int) -> Tuple[bool, int]: x, y = ctrl.mouse_pos() if ' ' in direction: modifier, direction = direction.split(' ', 1) if modifier == 'minor': distance *= 5 if modifier == 'major': distance *= 25 if direction == 'left': return True, x - distance elif direction == 'right': return True, x + distance elif direction == 'up': return False, y - distance elif direction == 'down': return False, y + distance raise ValueError(f"unsupported cardinal direction: {direction}")
def poll_mouse_pos(self): if self.canvas: pos = ctrl.mouse_pos() distance_threshold = 0.5 if self.smooth_mode else 20 if (self.prev_mouse_pos is None or numpy.linalg.norm( numpy.array(pos) - numpy.array(self.prev_mouse_pos)) > distance_threshold): self.prev_mouse_pos = pos if self.setup_type == "": self.x = pos[0] + self.limit_x self.y = pos[1] + self.limit_y self.canvas.move(self.x, self.y) self.determine_active_icon(pos) self.canvas.freeze()
def start_setup(self, setup_type, mouse_position=None): """Starts a setup mode that is used for moving, resizing and other various changes that the user might setup""" if (mouse_position is not None): self.drag_position = [ mouse_position[0] - self.limit_x, mouse_position[1] - self.limit_y ] if (setup_type not in self.allowed_setup_options and setup_type not in ["", "cancel", "reload"]): return pos = ctrl.mouse_pos() # Persist the user preferences when we end our setup if (self.setup_type != "" and not setup_type): self.drag_position = [] rect = self.canvas.rect if (self.setup_type == "position"): self.preferences.limit_x = int(rect.x) - pos[0] self.preferences.limit_y = int(rect.y) - pos[1] self.limit_x = self.preferences.limit_x self.limit_y = self.preferences.limit_y elif (self.setup_type == "dimension"): self.limit_x = self.preferences.limit_x self.limit_y = self.preferences.limit_y self.width = int(rect.width) self.height = int(rect.height) self.limit_width = int(rect.width) self.limit_height = int(rect.height) self.preferences.width = self.limit_width self.preferences.height = self.limit_height self.preferences.limit_width = self.limit_width self.preferences.limit_height = self.limit_height self.setup_type = setup_type self.preferences.mark_changed = True self.canvas.resume() self.event_dispatch.request_persist_preferences() # Cancel every change else: self.canvas.resume() super().start_setup(setup_type, mouse_position)
def open_dragon_pad(m): old = ctrl.mouse_pos() x = applescript.run(""" tell application "System Events" to tell process "Dragon" to tell (menu bar item 1 of menu bar 2) set AppleScript's text item delimiters to ", " position as string end tell """) x, y = map(int, x.split(", ")) ctrl.mouse(x, y) ctrl.mouse_click() ctrl.mouse(*old) applescript.run(""" tell application "System Events" to tell process "Dragon" to tell (menu bar item 1 of menu bar 2) click menu item "Help" of menu 1 click menu item "DragonPad" of menu of menu item "Help" of menu 1 end tell """)
def print_mouse_positions() -> None: """Print the mouse position relative to each corner. Use to get hard-codable positions. """ mouse_pos = ctrl.mouse_pos() print(f"Absolute mouse pos: {mouse_pos}") screen = ui.main_screen().rect print(f"Main screen: {screen}") for corner in [ Corner.TOP_LEFT, Corner.TOP_RIGHT, Corner.BOTTOM_LEFT, Corner.BOTTOM_RIGHT, ]: corner_pos = Corner.absolute_position(corner) relative = (mouse_pos[0] - corner_pos[0], mouse_pos[1] - corner_pos[1]) print(f"Position relative to {corner}: {relative}")
def internal(m): cur_x, cur_y = ctrl.mouse_pos() region = regions[m._words[1]] if region == "west": pos = (cur_x - no_pixels_int, cur_y) elif region == "east": pos = (cur_x + no_pixels_int, cur_y) elif region == "north": pos = (cur_x, cur_y - no_pixels_int) elif region == "south": pos = (cur_x, cur_y + no_pixels_int) elif region == "northeast": pos = (cur_x + no_pixels_int, cur_y - no_pixels_int) elif region == "northwest": pos = (cur_x - no_pixels_int, cur_y - no_pixels_int) elif region == "southeast": pos = (cur_x + no_pixels_int, cur_y + no_pixels_int) elif region == "southwest": pos = (cur_x - no_pixels_int, cur_y + no_pixels_int) ctrl.mouse_move(*pos)
def on_gaze(self, b): p = Point2d(*ctrl.mouse_pos()) on_main = is_on_main(p) if not control_mouse.enabled or not on_main or (mouse.last_ctrl and mouse.break_force > 6): self.cursor(True) ctrl.cursor_visible(True) else: try: # hides after every eye jump until a head movement origin = mouse.origin frames = [xy for xy in mouse.xy_hist if xy.ts >= origin.ts] m = max([(origin - xy).len() for xy in frames]) self.cursor(m > 5) return # this variant hides the cursor on every eye jump until it settles (can tweak radius up to 200) p, origin, radius = mouse.zone1 self.cursor(radius > 20) except Exception: self.cursor(True)
def update_regions(self): self.active_regions = [] if not self.enabled: self.regions = self.content.get_topic("screen_regions") return soft_enable = False indices_to_clear = [] region_indices_used = [] regions = self.content.get_topic("screen_regions") if regions is not None: new_regions = regions for index, canvas_reference in enumerate(self.canvases): region_found = False for region_index, region in enumerate(new_regions): # Move the canvas in case we are dealing with the same region if self.compare_regions(canvas_reference["region"], region): region_found = True region_indices_used.append(region_index) canvas_reference["canvas"].unregister( "draw", canvas_reference["callback"]) canvas_rect = self.align_region_canvas_rect(region) canvas_reference["canvas"].move( canvas_rect.x, canvas_rect.y) canvas_reference[ "callback"] = lambda canvas, self=self, region=region: self.draw_region( canvas, region) canvas_reference["region"] = region canvas_reference["canvas"].register( "draw", canvas_reference["callback"]) canvas_reference["canvas"].freeze() self.canvases[index] = canvas_reference if region_found == False: indices_to_clear.append(index) canvas_reference["canvas"].unregister( "draw", canvas_reference["callback"]) canvas_reference["region"] = None canvas_reference["canvas"] = None canvas_reference = None soft_enable = (self.regions != new_regions or not self.soft_enabled) and len(new_regions) > 0 self.regions = new_regions # Clear the canvases in reverse order to make sure the indices stay correct indices_to_clear.reverse() for index in indices_to_clear: self.canvases.pop(index) if len(self.regions) > 0: if soft_enable: self.soft_enable() self.activate_mouse_tracking() self.create_canvases(region_indices_used) self.determine_active_regions(ctrl.mouse_pos()) else: self.soft_disable()
def start_setup(self, setup_type, mouse_position=None): """Starts a setup mode that is used for moving, resizing and other various changes that the user might setup""" if (mouse_position is not None): self.drag_position = [ mouse_position[0] - self.limit_x, mouse_position[1] - self.limit_y ] if (setup_type not in self.allowed_setup_options and setup_type not in ["", "cancel", "reload"]): return # Persist the user preferences when we end our setup if (self.setup_type != "" and not setup_type): self.drag_position = [] rect = self.canvas.rect if (self.setup_type == "position"): self.preferences.x = int( rect.x) if self.limit_x == self.x else int(rect.x - (self.limit_x - self.x)) self.preferences.y = int( rect.y) if self.limit_y == self.y else int(rect.y - (self.limit_y - self.y)) self.preferences.limit_x = int(rect.x) self.preferences.limit_y = int(rect.y) elif (self.setup_type == "dimension"): self.preferences.x = int(rect.x) self.preferences.y = int(rect.y) self.preferences.width = int(rect.width) self.preferences.height = int(rect.height) self.preferences.limit_x = int(rect.x) self.preferences.limit_y = int(rect.y) self.preferences.limit_width = int(rect.width) self.preferences.limit_height = int(rect.height) elif (self.setup_type == "limit"): self.preferences.x = self.x self.preferences.y = self.y self.preferences.width = self.width self.preferences.height = self.height self.preferences.limit_x = int(rect.x) self.preferences.limit_y = int(rect.y) self.preferences.limit_width = int(rect.width) self.preferences.limit_height = int(rect.height) elif (self.setup_type == "font_size"): self.preferences.font_size = self.font_size self.setup_type = setup_type self.preferences.mark_changed = True if self.canvas: self.canvas.resume() self.event_dispatch.request_persist_preferences() # Cancel every change elif setup_type == "cancel": self.drag_position = [] if (self.setup_type != ""): self.load({}, False) self.setup_type = "" if self.canvas and self.enabled: rect = ui.Rect(self.x, self.y, self.width, self.height) self.canvas.rect = rect self.canvas.resume() elif setup_type == "reload": self.drag_position = [] self.setup_type = "" if self.canvas and self.enabled: rect = ui.Rect(self.x, self.y, self.width, self.height) # Only do a rect change if it has actually changed to prevent costly operations if self.canvas.rect.x != self.x or self.canvas.rect.y != self.y or \ self.canvas.rect.width != self.width or self.canvas.rect.height != self.height: self.canvas.rect = rect self.canvas.resume() # Start the setup state elif self.setup_type != setup_type: self.setup_type = setup_type x, y = ctrl.mouse_pos() center_x = self.x + (self.width / 2) center_y = self.y + (self.height / 2) # Determine the direction of the mouse from the widget direction = Point2d(x - center_x, y - center_y) self.setup_horizontal_direction = "left" if direction.x < 0 else "right" self.setup_vertical_direction = "up" if direction.y < 0 else "down" if (self.setup_type != ""): self.setup_move(ctrl.mouse_pos())
def click(): x, y = ctrl.mouse_pos() ctrl.mouse_click(pos=(x, y), button=0, times=1, wait=16000)
def _record_mouse_position(self): timestamp = time.time() position = ctrl.mouse_pos() self.history.append((position[0], position[1], timestamp))
def check_mouse(self): pos = ctrl.mouse_pos() if pos != self.last_pos: x, y = pos self.canvas.move(x - self.width // 2, y - self.height // 2) self.last_pos = pos
def navigate_right(m): press('ctrl-cmd-shift-l') (x, y) = ctrl.mouse_pos() ctrl.mouse(x + distance_to_navigate, y)
def copy_mouse_position(): """Copy the current mouse position coordinates""" position = ctrl.mouse_pos() clip.set_text((repr(position)))
def start_setup(self, setup_type): """Starts a setup mode that is used for moving, resizing and other various changes that the user might setup""" if (setup_type not in self.allowed_setup_options and setup_type not in ["", "cancel"]): return # Persist the user preferences when we end our setup if (self.setup_type != "" and not setup_type): rect = self.canvas.get_rect() if (self.setup_type == "position"): self.preferences.x = int( rect.x) if self.limit_x == self.x else int(rect.x - (self.limit_x - self.x)) self.preferences.y = int( rect.y) if self.limit_y == self.y else int(rect.y - (self.limit_y - self.y)) self.preferences.limit_x = int(rect.x) self.preferences.limit_y = int(rect.y) elif (self.setup_type == "dimension"): self.preferences.x = int(rect.x) self.preferences.y = int(rect.y) self.preferences.width = int(rect.width) self.preferences.height = int(rect.height) self.preferences.limit_x = int(rect.x) self.preferences.limit_y = int(rect.y) self.preferences.limit_width = int(rect.width) self.preferences.limit_height = int(rect.height) elif (self.setup_type == "limit"): self.preferences.x = self.x self.preferences.y = self.y self.preferences.width = self.width self.preferences.height = self.height self.preferences.limit_x = int(rect.x) self.preferences.limit_y = int(rect.y) self.preferences.limit_width = int(rect.width) self.preferences.limit_height = int(rect.height) elif (self.setup_type == "font_size"): self.preferences.font_size = self.font_size self.setup_type = setup_type self.preferences.mark_changed = True self.canvas.resume() actions.user.persist_hud_preferences() # Cancel every change elif setup_type == "cancel": if (self.setup_type != ""): self.load({}, False) if self.canvas: rect = ui.Rect(self.x, self.y, self.width, self.height) self.canvas.set_rect(rect) self.setup_type = "" self.canvas.resume() # Start the setup state elif self.setup_type != setup_type: self.setup_type = setup_type x, y = ctrl.mouse_pos() center_x = self.x + (self.width / 2) center_y = self.y + (self.height / 2) # Determine the direction of the mouse from the widget direction = Point2d(x - center_x, y - center_y) self.setup_horizontal_direction = "left" if direction.x < 0 else "right" self.setup_vertical_direction = "up" if direction.y < 0 else "down" if (self.setup_type != ""): self.setup_move(ctrl.mouse_pos())
def _store_pre_position(*args): """Store the mouse position at phrase start.""" global PHRASE_START_POSITION, PHRASE_START_POS_LOCK with PHRASE_START_POS_LOCK: PHRASE_START_POSITION = ctrl.mouse_pos()
def get_current_mouse_pos(m): return ctrl.mouse_pos()
def start_setup(self, setup_type, mouse_position=None): """Starts a setup mode that is used for moving, resizing and other various changes that the user might setup""" if (mouse_position is not None): self.drag_position = [ mouse_position[0] - self.limit_x, mouse_position[1] - self.limit_y ] if (setup_type not in self.allowed_setup_options and setup_type not in ["", "cancel", "reload"]): return pos = ctrl.mouse_pos() # Persist the user preferences when we end our setup if (self.setup_type != "" and not setup_type): self.drag_position = [] rect = self.canvas.rect if (self.setup_type == "dimension"): self.x = 0 self.y = 0 self.width = int(rect.width) self.height = int(rect.height) self.limit_width = int(rect.width) self.limit_height = int(rect.height) self.preferences.width = self.limit_width self.preferences.height = self.limit_height self.preferences.limit_width = self.limit_width self.preferences.limit_height = self.limit_height elif (self.setup_type == "font_size"): self.preferences.font_size = self.font_size self.setup_type = setup_type self.preferences.mark_changed = True self.canvas.pause() self.canvas.unregister("draw", self.setup_draw_cycle) self.canvas = None self.event_dispatch.request_persist_preferences() # Cancel every change elif setup_type == "cancel": self.drag_position = [] if (self.setup_type != ""): self.load({}, False) self.setup_type = "" if self.canvas: self.canvas.unregister("draw", self.setup_draw_cycle) self.canvas = None for canvas_reference in self.canvases: canvas_rect = self.align_region_canvas_rect( canvas_reference["region"]) canvas_reference["canvas"].rect = canvas_rect canvas_reference["canvas"].freeze() elif setup_type == "reload": self.drag_position = [] self.setup_type = "" for canvas_reference in self.canvases: canvas_reference["canvas"].freeze() # Start the setup by mocking a full screen screen region to place the canvas in else: main_screen = ui.main_screen() region = HudScreenRegion("setup", "Setup mode text", "command_icon", "DD4500", main_screen.rect, \ Point2d(main_screen.rect.x, main_screen.rect.y)) region.vertical_centered = True canvas_rect = self.align_region_canvas_rect(region) self.x = canvas_rect.x self.y = canvas_rect.y if not self.canvas: self.canvas = canvas.Canvas(self.x, self.y, self.limit_width, self.limit_height) self.canvas.register("draw", self.setup_draw_cycle) self.canvas.move(self.x, self.y) self.canvas.resume() super().start_setup(setup_type, mouse_position)
def log_cursor(self): """Store the most most recent mouse position.""" if len(self.cursor_log) > self.max_log_size: self.cursor_log.pop(0) self.cursor_log.append(ctrl.mouse_pos())
def nudge_mouse(x: int, y: int): """Move the mouse relatively""" _x, _y = ctrl.mouse_pos() ctrl.mouse_move(_x + x, _y + y)
def mouse_capture_coordinates(): """copy the current coordinate tuple to the clipboard""" print(ctrl.mouse_pos()) x, y = ctrl.mouse_pos() clip.set_text(f"{x},{y}")
def navigate_left(m): press('ctrl-cmd-shift-h') (x, y) = ctrl.mouse_pos() ctrl.mouse(x - distance_to_navigate, y)
def on_gaze(self, frame): l, r = frame.left, frame.right # print("GGG",frame.gaze) # print(l, r) pos = frame.gaze pos *= self.size_px self.current_time = int(self.get_time()) # randomly flickers 0 and 1 so needs a filter # glitchy, also mouse_scroll "down" key is lame cheat, im sure talon has a 'scroll x' # print("POS",pos) # print(frame.left.gaze.x, frame.right.gaze.x) # print("Present: ",frame.left.detected, frame.right.detected)#not reliable if not (pos.x <= 0 and pos.y <= 0): next(self.right_history) self.right_open = self.right_history.send(frame.right.detected) next(self.left_history) self.left_open = self.left_history.send(frame.left.detected) # Eye signal self.nosignal = 0 self.signal += 1 if self.signal < 3: # 4: return # print("Open your eyes more between blinks!"); return if self.eyes == False: if self.two_blinks: if abs(self.current_time - self.first_blink) > self.expire: # Second blink, but expired. print("Click expired:", self.current_time - self.first_blink) self.first_blink = self.current_time self.second = False if not self.second: try: # print("[First Blink] Started") try: self.target_history = self.eye_history[ -1 * self.magic] except Exception as e: print(e) self.target_history = self.eye_history[len( self.eye_history) * -1] fx, fy = self.estimate_focus( self.eye_history, self.magic) # print("Focus:",fx,fy,self.target_history) self.target_x, self.target_y = ( self.target_history[0], self.target_history[1], ) # print(abs(fx-self.target_x),abs(fy-self.target_y)) if (abs(fx - self.target_x) > self.focus_sensitivity or abs(fy - self.target_y) > self.focus_sensitivity): print( "Misclick assumed, not focusing enough or change params." ) print(abs(fx - self.target_x), abs(fy - self.target_y)) self.first_blink = (self.current_time - 15000 ) # expires cheap method if self.beep: self.beep_win(3500, 20) else: self.first_blink = self.current_time # if self.beep: self.beep_win(8000,50) #debug self.second = True except Exception as e: print("ERR", e) else: print("Second blink click going through: ", pos) ctrl.mouse_move(self.target_x, self.target_y) if self.beep: self.beep_win() if self.left_closed_count > 5 or self.right_closed_count > 5: pass # print("Eyes are closed oddly..") else: # print("Allowing it") # ctrl.mouse_click( # pos=(self.target_x, self.target_y), hold=32000 # ) # app.notify(subtitle="blink click") pass # ctrl.mouse_click(pos=(self.target_x, self.target_y), hold=32000) self.second = False self.eyes = True px, py = ctrl.mouse_pos() self.hist_add(self.current_time, [px, py]) # Eye scroll left self.eye_scroll_right(frame) self.eye_scroll_left(frame) else: self.signal = 0 if self.nosignal == 0: self.eyes = False self.nosignal += 1
import eye_mouse import time from talon import ctrl, tap from talon.voice import Context ctx = Context('mouse') x, y = ctrl.mouse_pos() mouse_history = [(x, y, time.time())] force_move = None def on_move(typ, e): mouse_history.append((e.x, e.y, time.time())) if force_move: e.x, e.y = force_move return True tap.register(tap.MMOVE, on_move) def click_pos(m): word = m._words[0] start = (word.start + min((word.end - word.start) / 2, 0.100)) / 1000.0 diff, pos = min([(abs(start - pos[2]), pos) for pos in mouse_history]) return pos[:2] def delayed_click(m, button=0, times=1): old = eye_mouse.config.control_mouse eye_mouse.config.control_mouse = False x, y = click_pos(m) ctrl.mouse(x, y) ctrl.mouse_click(x, y, button=button, times=times, wait=16000) time.sleep(0.032) eye_mouse.config.control_mouse = old
def reposition_canvas(): x, y = ctrl.mouse_pos() print(x, y) can.move(x, y)