Saturday, November 30, 2013

Activate specific window on mouse over

I use three monitors with two of them used for the host (Win7) and one for linux (fullscreened VM). I grew tired of having to click inside the VM window to be able to send input to it so I chopped together this AutoHotkey script that will activate it on mouse over. This mimics how a separate PC setup with its own monitor and Synergy works and feels a lot more... correct/intuitive/good/blargh than having to click.

I'm not sure how robust the activation of the previously focused window on mouse out is. The solutions for this kind of problem I've been able to find have been constructed for use with all windows not just a single specific one. I guess that might warrant more checks to actually get the right previous window and the right Z value and what not. I couldn't be bothered to actually find out why and this apparently works so... whatever. :)

edit (2013-12-07): Simplified the script... not sure why I thought the second timer was necessary. Now simply checks if cursor has moved and if so calls the title-check-activate-sub. And yes, I do realize that setting the ExStyle on every hover is a bit excessive but it is the lazy approach to the problem, hehe.

And now, after some swearing I have found out that the ^ in the WinSet command is used to toggle the style which is not what we want. Replaced with the proper +, yay. That's what you get for copy-pasting and not researching on you own, I know it but I still do it, pfft.
#SingleInstance force
#Persistent
#NoEnv

;// ---------------------------------
;// Init
;// ---------------------------------
SetWinDelay, -1
CoordMode, Mouse, Screen

vm_title := "name_of_VM [Running] - Oracle VM VirtualBox"

SetTimer, HoverCheck, 100
return


;// ---------------------------------
;// Activate VM on hover
;// ---------------------------------
HoverCheck:
  MouseGetPos, x, y
  if (x != lastx or y != lasty)
  {
    GoSub, hovering
    lastx := x
    lasty := y
  }
return

Hovering:
  MouseGetPos,,, win
  WinGetTitle, tl, ahk_id %win%

  if (tl = vm_title) {
    if (!WinActive("ahk_id " win)) {
      WinGet, prevWin, ID, A
      WinActivate, ahk_id %win%
      WinSet, ExStyle, +0x80,  ahk_id %win% ;// hides the VM window from alt+tab
    }
  } else if (prevWin != "") {
    WinActivate, ahk_id %prevWin%
    prevWin := ""
  }
return