How to set window to always on top using AutoHotKey v2?

5k views Asked by At

I need to write a script to make the active window always on top, but all the examples are for AutoHotKey v1. How can I do this with AHK v2?

2

There are 2 answers

0
Ethan Malloy On BEST ANSWER

Use this (Hotkey is CTRL + SPACE):

^space::
{
    WinSetAlwaysOnTop -1, "A"
}

How it works:

This script uses the WinSetAlwaysOnTop method to accomplish its goal. It takes several parameters, but not all of them are mandatory. -1 tells the function to toggle the setting to the opposite of whatever it was (e.g. OFF --> ON), and "A" tells it to use the currently active window (whichever window has focus). You can also pass it the name of a specific window, like "Calculator".

Here are some links to the relevant documentation:

0
xypha On

Use below code to toggle 'Always on top' state and alter the window title when a window's "On Top" status is changed -

#Requires AutoHotkey v2.0

!t:: {                          ; Alt + t
Title_When_On_Top := "! "       ; change title "! " as required
t := WinGetTitle("A")
ExStyle := WinGetExStyle(t)
If (ExStyle & 0x8) {            ; 0x8 is WS_EX_TOPMOST
    WinSetAlwaysOnTop 0, t      ; Turn OFF and remove Title_When_On_Top
    WinSetTitle (RegExReplace(t, Title_When_On_Top)), t 
    }
Else {
    WinSetAlwaysOnTop 1, t      ; Turn ON and add Title_When_On_Top
    WinSetTitle Title_When_On_Top t, t
    }
}

Modified from AHK v1 code.