Well, this is just the right script for you!
Here's how it works: pressing "P" while the right Windows key is held will label the active window as "- AlwaysOnTop". Even if you tab into a different window, the pinned window stays on top of the stack.
Using the same key combination will unpin the window, and pin the currently active window (if applicable).
I've included a script file, so those interested may download it. Hopefully you all can learn from tweaking this function as I have!
Code: Select all
;------Hotkey------
<#p:: ;Win+P
pinWindow()
RETURN
;------------------
;---------------Function---------------
;Makes the active window AlwaysOnTop
pinWindow(targetWindow := "A")
{
static pWnd := 0
static pinned := FALSE
tWnd := WinActive(targetWindow) ;Defines %tWnd% as the ahk_id for the target window
IF (pinned NOT TRUE)
{
WinSet, AlwaysOnTop, Toggle, % "ahk_id " tWnd ;Sets the target window as AlwaysOnTop
WinGetTitle, title, % "ahk_id " tWnd ;Retrieves the title for the target window.
IF title ;Runs code if title isn't blank.
{
Gosub ChangeTitle
}
pinned := TRUE ;Used for UnpinWindow logic
pWnd := tWnd ;Used for unpinning window before pinning the next window.
}
ELSE
{
Gosub UnpinWindow
}
ChangeTitle:
WinGet, ExStyle, ExStyle, % "ahk_id " tWnd ;Checks the style of the target window.
IF (ExStyle & 0x8) ;0x8 is for AlwaysOnTop
{
WinSetTitle, % "ahk_id " tWnd,, %title% - AlwaysOnTop ;Adds "- AlwaysOnTop" to the window title.
}
ELSE
{
WinSetTitle, % "ahk_id " tWnd,, % RegexReplace(title, " - AlwaysOnTop") ;Removes "- AlwaysOnTop" to the window title.
}
RETURN
UnpinWindow:
WinSet, AlwaysOnTop, Off, % "ahk_id " pWnd ;Sets the target window as AlwaysOnTop
WinGetTitle, title, % "ahk_id " pWnd ;Retrieves the title for the target window.
WinSetTitle, % "ahk_id " pWnd,, % RegexReplace(title, " - AlwaysOnTop") ;Removes indicator from the window title.
IF (tWnd != pWnd)
{
WinSet, AlwaysOnTop, Toggle, % "ahk_id " tWnd ;Sets the target window as AlwaysOnTop
WinGetTitle, title, % "ahk_id " tWnd ;Retrieves the title for the target window.
IF title
{
Gosub ChangeTitle
}
pWnd := tWnd
}
ELSE
{
pinned := FALSE
}
RETURN
}
;--------------------------------------