Question

I have found the following script for dynamically assigning hotkeys to already open windows:

Code (Expand):
Loop 10
{
   i := A_Index - 1
   HotKey #^%i%,DynHotkey
   HotKey #%i%, DynHotkey
   HotKey #!%i%,DynHotkey
}
Exit

DynHotkey:
   StringRight i, A_ThisHotKey, 1
   StringMid what,A_ThisHotKey, 2, 1
   var := var%i%
   IfEqual what, ^, WinGet var%i%, ID, A  ; Save ID
   Else IfEqual what,!, WinMinimizeAll    ; MinimizeAll
   WinRestore  ahk_id %var%
   WinActivate ahk_id %var%               ; Switch
Return

(the code was copied from this thread http://www.autohotkey.com/forum/topic38773.html&highlight=dynamic+hot+key)

With the above script you can:

  1. Use Win+Ctrl+0..9 to attach hotkey to current active window.
  2. Use Win+0..9 to switch to correspoding window.

However, if I assign a hotkey to a given window (using Win+Ctrl+0..9), and then I want I want to go back to that window (Win+0..9), the window is reset to a new size & location.

Is there way of saving the size & location of the window along with it's ID?

If so, what would the script look like?

I am running the above script on Windows 7 64-bit.

Thanks a lot,

Was it helpful?

Solution

You dont need to complicate the code :)

Quick question: if your window is minimized you dont have any problems right? The "problem" on the code is the WinRestore.

The thing is that if the window is not minimized and then you do a WinRestore it will change the size and position to the "not maximized" version of it.

WinActivate automatically does a WinRestore only if the window is minimized, so you can safely remove line 16 (the WinRestore one) since WinActivate will do what you need.

--edit--

this is how the code should look:

Loop 10
{
    i := A_Index - 1
    HotKey #^%i%,DynHotkey
    HotKey #%i%, DynHotkey
    HotKey #!%i%,DynHotkey
}
Exit

DynHotkey:
    StringRight i, A_ThisHotKey, 1
    StringMid what,A_ThisHotKey, 2, 1
    var := var%i%
    IfEqual what, ^, WinGet var%i%, ID, A  ; Save ID
    Else IfEqual what,!, WinMinimizeAll    ; MinimizeAll
    WinActivate ahk_id %var%               ; Switch
Return

I tested it, it works perfectly.

OTHER TIPS

You can use WinGetPos to read the actual position and save it. Then you can use WinMove to set the position. Here is a function list: http://www.autohotkey.com/docs/commands.htm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top