Tuesday, July 2, 2024

Is there any approach to trick common management to redirect keyboard enter to Mac #2 after utilizing a script to maneuver mouse from Mac #1 to Mac #2?

I would like to have the ability to transfer the cursor to my different laptop utilizing a scorching key when utilizing Common Management.

So I devised a hack for tricking Common Management into giving the opposite laptop mouse management. The hack depends on an accessibility setting which lets you management the pointer with the keyboard. The hack makes use of a python script triggered with a hotkey. The script runs on laptop #1 and strikes the pointer to the second machine. Script #1 then triggers a second python script on laptop #2 which takes over transfer the mouse to the center of the display screen and clicks. This is some extra particulars of the operation:

  1. The script on laptop #1 strikes the pointer to the very backside of the monitor. The monitor for laptop #1 sits bodily above laptop #2.
  2. The script then hits numpad 2 twice. This strikes the cursor down.
  3. Now the pointer reveals up on the highest of laptop #2’s monitor.
  4. Laptop #1 then sends a command to laptop #2 over ssh to run a python script that transfer the mouse round and click on to wherever.
  5. In any case this, I can transfer the pointer round on laptop #2 as if I had moved it there manually. Nice!

In case you have an interest, this is the script for Laptop #1:

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

pyautogui.moveTo(-463, 1439.99)

keyboard = Controller()
instances = 3 # Variety of instances to press the important thing
delay = 0.02  # Delay between key presses in seconds

for _ in vary(instances):
    keyboard.press('2')
    keyboard.launch('2')

    time.sleep(delay)  # Await a bit earlier than subsequent press

And for Laptop #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.measurement()

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

# print the middle coordinates
print(center_x, center_y)

# discover the middle of the display screen and transfer 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):
    # Transfer the cursor to the specified place
    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)

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

All the pieces works fairly nicely. Nevertheless, the keyboard nonetheless directs enter to Laptop #1, even after clicking on Laptop #2 with the script above. I hoped utilizing a decrease degree script with Quartz (in script #2) would make a distinct nevertheless it did not.

In consequence. I nonetheless need to manually click on the trackpad to redirect keystrokes to laptop #2.

Does anybody know of anyway I would trick Common Management into sending keystrokes to Laptop #2 instantly with out having to click on on the trackpad myself?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles