Restoring a previously saved window position Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Restoring a previously saved window position

Post by Lem2001 » 13 Dec 2022, 07:26

I'm using WinGetPos to obtain the coordinates and dimensions of the current window for various window transformations.

Those all work fine, but I'd like to add the option for a separate hotkey that can undo the last window transform by returning the window to its previous state (as determined by WinGetPos). The idea is to be able to use hotkeys to freely toggle back and forth between the current and previous window states.

What's the best way to go about this?

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Restoring a previously saved window position

Post by mikeyww » 13 Dec 2022, 07:53

When you get the position, store the four dimensions in variables. You can then use them with WinMove.

Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Re: Restoring a previously saved window position

Post by Lem2001 » 13 Dec 2022, 09:09

Yes, I know that. As I stated in my original post, that part is already working fine.

The issue that I'm having is using those same dimensions (that were collected by hotkey1) in a different hotkey (hotkey2) which can then toggle back and forth between the two states by repeated pressing of hotkey2.

When I try to use the variable that was assigned to hold the dimensions in the first hotkey, it's empty when it's accessed in the second hotkey. I also can't figure out how to swap the two settings back and forth on subsequent presses of the same key (hotkey2).

So to recap and hopefully clarify:

The initial hotkey (hotkey1) captures current window dimensions, and then performs a transformation of position and size. If I decide that I don't like it and therefore want to undo it (or if I want to compare the transformed and pre-transformed positions by swapping back and forth between them) then I'd like to use a different hotkey (hotkey2) that acts as an 'Undo'. This second hotkey will save the current transformed state (easy to do) but then I want it to restore the pre-transformed state that was saved when hotkey1 was initially pressed (that's the bit I can't do).

After that, any subsequent presses of hotkey2 will toggle back and forth between the current transformed state (captured when hotkey2 was first pressed) and the pre-transformed state (that's read from the value captured when hotkey1 was pressed).

The functionality of hotkey1 is already done and working, so I have a variable with the 'pre-transformed' dimensions. What I would like help with is getting the second hotkey to read the pre-transformed state (that was captured by hotkey1) and then toggle between them each time that hotkey2 is pressed.

Thanks.

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Restoring a previously saved window position  Topic is solved

Post by mikeyww » 13 Dec 2022, 09:32

Code: Select all

Global prev := {}
F3::move(100, 100, 500, 500)
F5::move() ; Undo

move(x2 := "", y2 := "", w2 := "", h2 := "", hWnd := "") {
 (!hWnd) && hWnd := WinActive("A")
 WinGetPos, x1, y1, w1, h1, % wTitle := "ahk_id" hWnd ; Current position
 (x2 = "") && x2 := prev[hWnd].x                      ; New position
 (y2 = "") && y2 := prev[hWnd].y
 (w2 = "") && w2 := prev[hWnd].w
 (h2 = "") && h2 := prev[hWnd].h
 WinMove, %wTitle%,, x2, y2, w2, h2                   ; Move to new position
 prev[hWnd] := {x: x1, y: y1, w: w1, h: h1}           ; Set previous position
}

Post Reply

Return to “Ask for Help (v1)”