0

I want to be able to move the cursor to my other computer using a hot key when using Universal Control.

So I devised a hack for tricking Universal Control into giving the other computer mouse control. The hack relies on an accessibility setting which allows you to control the pointer with the keyboard. The hack uses a python script triggered with a hotkey. The script runs on computer #1 and moves the pointer to the second machine. Script #1 then triggers a second python script on computer #2 which takes over move the mouse to the middle of the screen and clicks. Here's some more details of the operation:

  1. The script on computer #1 moves the pointer to the very bottom of the monitor. The monitor for computer #1 sits physically above computer #2.
  2. The script then hits numpad 2 twice. This moves the cursor down.
  3. Now the pointer shows up on the top of computer #2's monitor.
  4. Computer #1 then sends a command to computer #2 over ssh to run a python script that move the mouse around and click to wherever.
  5. After all this, I can move the pointer around on computer #2 as if I had moved it there manually. Great!

In case you are interested, here's the script for Computer #1:

from pynput.keyboard import Key, Controller, KeyCode
import time, pyautogui

pyautogui.moveTo(-463, 1439.99)

keyboard = Controller()
times = 3 # Number of times to press the key
delay = 0.02  # Delay between key presses in seconds

for _ in range(times):
    keyboard.press('2')
    keyboard.release('2')

    time.sleep(delay)  # Wait for a bit before next press

And for Computer #2:

#!/usr/bin/env python3
from pynput.keyboard import Key, Controller, KeyCode
import pyautogui
from Quartz.CoreGraphics import CGEventCreateMouseEvent, kCGEventMouseMoved, kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGEventSourceStateHIDSystemState, kCGMouseButtonLeft, CGEventPost, kCGHIDEventTap


screen_width, screen_height = pyautogui.size()

# Calculate the center coordinates
center_x = screen_width // 2
center_y = screen_height // 2

# print the center coordinates
print(center_x, center_y)

# find the center of the screen and move to it
pyautogui.moveTo(center_x, center_y)
my_move = 400
pyautogui.moveTo(center_x, center_y + my_move)

def simulate_trackpad_click(x, y):
    # Move the cursor to the desired position
    event_move = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (x, y), kCGMouseButtonLeft)
    CGEventPost(kCGEventSourceStateHIDSystemState, event_move)

    # Simulate left mouse button down
    event_down = CGEventCreateMouseEvent(None, kCGEventLeftMouseDown, (x, y), kCGMouseButtonLeft)
    CGEventPost(kCGEventSourceStateHIDSystemState, event_down)

    # Simulate left mouse button up
    event_up = CGEventCreateMouseEvent(None, kCGEventLeftMouseUp, (x, y), kCGMouseButtonLeft)
    CGEventPost(kCGEventSourceStateHIDSystemState, event_up)

# Usage
simulate_trackpad_click(center_x - 200, center_y + my_move)
simulate_trackpad_click(center_x - 200, center_y + my_move)

Everything works pretty well. However, the keyboard still directs input to Computer #1, even after clicking on Computer #2 with the script above. I was hoping using a lower level script with Quartz (in script #2) would make a different but it didn't.

As a result. I still have to manually click the trackpad to redirect keystrokes to computer #2.

Does anyone know of anyway I might trick Universal Control into sending keystrokes to Computer #2 immediately without having to click on the trackpad myself?

4
  • You have to manually click because a window needs focus for keyboard input to function
    – Allan
    Commented Mar 16 at 19:27
  • Yeah, I'm look for some way to give the second machine focus programmatically.
    – StevieD
    Commented Mar 16 at 19:30
  • Can you edit your question so you’re focused on that? We don’t need all the code for what is already working. It introduces a lot of confusion
    – Allan
    Commented Mar 16 at 19:35
  • No. It's pretty straightforward and it provides necessary context.
    – StevieD
    Commented Mar 16 at 19:36

0

You must log in to answer this question.

Browse other questions tagged .