AutoHotkey Community

It is currently May 27th, 2012, 5:12 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: December 16th, 2010, 2:30 am 
Offline

Joined: April 1st, 2008, 4:37 pm
Posts: 24
Location: Netherlands
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: http://synergy2.sourceforge.net/ to share my mouse/keyword
I use http://sourceforge.net/projects/synergykm/ 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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 16th, 2010, 11:03 am 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
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.

Code:
#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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 16th, 2010, 3:16 pm 
Offline

Joined: April 1st, 2008, 4:37 pm
Posts: 24
Location: Netherlands
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:


Code:
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 16th, 2010, 7:49 pm 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
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.
http://www.autohotkey.com/docs/Hotkeys.htm#Context

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.

http://www.autohotkey.com/docs/commands/IfWinExist.htm
http://www.autohotkey.com/docs/commands/Gui.htm
http://www.autohotkey.com/docs/commands/WinSetTitle.htm


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: XstatyK and 69 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group