Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Detect different computer with Synergy and AHK


  • Please log in to reply
3 replies to this topic
Veil
  • Members
  • 24 posts
  • Last active: Jun 01 2013 04:47 PM
  • Joined: 01 Apr 2008
Hey all,

Setup
1 PC + Apple Keyboard
1 MacBook

I wrote an AHK script for Windows to switch some keys around so the shortcuts work the same as on my Mac (cmd+c instead of ctrl+c, etc). This works great.

I also have a Synergy server running on the Windows PC: <!-- m -->http://synergy2.sourceforge.net/<!-- m --> to share my mouse/keyword
I use <!-- m -->http://sourceforge.n...ects/synergykm/<!-- m --> on the Mac to connect to the PC to use its mouse/keyboard

Problem
Now, whenever I move my PC mouse out of the screen on Windows, it shows up on my Mac, and I can use my PC mouse and keyboard on that. The problem is that I swapped the keys around, and now they are wrong on the Mac (e.g. I need to use ctrl+c for cmd+c).

Question
Does anyone know how I can detect that I moved my mouse out of the screen and onto the other computer? If I can detect that change, I can simply disable the AHK rewrites when Synergy activates the keyboard/mouse on my Mac, and re-activate it when I go back to the PC.

I just can't figure out how to detect this. The log (Active Window Info) doesn't show anything about moving out of the screen. I also tried the bottom example script from SysGet, but it (obviously) still shows the same monitor because the mouse only moves 'away' over the LAN, not to another Windows monitor.

All help is much appreciated!

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
Hi Veil,

I have heard of Synergy but never used it.
Perhaps the mouse cursor location is usable ?

Here is a script that continuously reports the mouse location.
See what you get when the cursor move over to the Mac window.

#singleinstance force
#Persistent

if 0	; change to 1 for relative to active window or 0 for screen
{
  CoordMode, ToolTip, Relative 
  CoordMode, Mouse, Relative 
  CoordMode, Pixel, Relative 
  msgbox ,,,relative,1
}
else
{
  CoordMode, ToolTip, screen
  CoordMode, Mouse, screen
  CoordMode, Pixel, screen 
  msgbox ,,,screen,1
}

SetTimer, WatchCursor, 100
return

WatchCursor:
MouseGetPos, xpos, ypos, id, control
ToolTip, The mouse cursor is at `nX%xpos% Y%ypos%`nHit F1 to copy this info to clipboard, 200, 300
return

f1::
clipboard=The mouse cursor is at X%xpos% Y%ypos%
return

esc:: exitapp ; in case of emergency hit escape to exit


Veil
  • Members
  • 24 posts
  • Last active: Jun 01 2013 04:47 PM
  • Joined: 01 Apr 2008
Hi Leef_me,

Thanks for the script! I noticed that the cursor on the PC is set to the middle of the screen when I move the mouse to the Mac. Since it's very unlikely that I go to that exact position very often on the PC itself, that'd be a good way to tell where my mouse is.

I rewrote the script a bit, so it checks whether the cursor is in the middle or not:


SetTimer, WatchCursor, 1000
return

; start with PC (1 = PC, 2 = Mac)
; using Env var so it doesn't have to update constantly
EnvSet, MOUSELOC, 1

WatchCursor:
MouseGetPos, xpos, ypos, id, control

; get current location
EnvGet,MouseLocation, MOUSELOC

; do nothing by default
switchTo = 0

if (xpos == 840 && ypos == 525) {
  ; on Mac, if still set to PC, change
  if (MouseLocation == 1) {
    EnvSet, MOUSELOC, 2
    switchTo = 2 ; to Mac settings
  }
} else {
  ; on PC, if still set to Mac, change
  if (MouseLocation == 2) {
    EnvSet, MOUSELOC, 1
    switchTo = 1 ; to PC settings
  }  
}

if (switchTo > 0) {
  MsgBox Switch to: %switchTo%
}

return

This is just to test, but it seems to work great. I'll replace the MsgBox with the real scripts I want to switch later on. It only shows the MsgBox once when the position of the cursor goes from the middle of my screen to something else. That way it doesn't have to switch every time I move my mouse.

Last question
This will only work if it constantly keeps track of my mouse position. I set the function timeout to 1 second instead of 100ms. Would this delay be big enough? I guess 5 sec would be fine too. But I'm a bit worried that this would cause unnecessary load on the system? Or is tracking the cursor a non-issue?

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
In response to what value for Settimer, you'll have to decide.

I would think that 5 seconds is too long. I typically use 100ms because I need active monitoring of the mouse location.
However, all scripts I have written thus-far have a rather short operational period, not 'all day, every day'.

>>EnvGet,MouseLocation, MOUSELOC

btw, unless you have another program that reads MOUSELOC, there is no need for EnvGet/EnvSet; a simple variable assignment would be faster.

I had an idea some time ago and it worked, to change hotkeys on a context basis.
<!-- m -->http://www.autohotke...eys.htm#Context<!-- m -->

The idea is to create your own window (that can be invisible to user).
When there is some need to change what hotkeys are active, the name of the gui is changed, and AHk auto-switches to use the new hotkeys.

<!-- m -->http://www.autohotke.../IfWinExist.htm<!-- m -->
<!-- m -->http://www.autohotke...ommands/Gui.htm<!-- m -->
<!-- m -->http://www.autohotke...WinSetTitle.htm<!-- m -->