1
votes

How to use AHK to activate Windows10 Calculator app that is in AlwaysOnTop mode
If you click on 'Keep on top' when Calculator is in Standard mode it becomes something like this

enter image description here

Note that it has no title bar
The AHK script I use to activate an already open Calculator is no longer working

#c::
    if not WinExist("Calculator")
    {
        Run calc.exe
        WinWait Calculator
    }
    WinActivate Calculator

I tried tips from How to get window handle by PID in autohotkey?
But I am not getting a valid class id, this is my script with some debugging added:

#c::
    DetectHiddenWindows, on

    Process, Exist, Calculator.exe
    cpid := ErrorLevel    
    WinGetClass, ClassID, ahk_pid %cpid%
    WinGetTitle, Title, ahk_pid %cpid%
    exist := WinExist("ahk_exe Calculator.exe")
    active := WinActive("ahk_exe Calculator.exe")
    MsgBox look for [%cpid%] [%Title%] [%ClassID%] [%exist%] [%active%] ; cpid is the only valid variable

    if not WinExist("Calculator")
    {
        Run calc.exe
        WinWait Calculator
    }

    WinActivate Calculator
2

2 Answers

1
votes

The Window doesn't come from Calculator.exe, it comes from ApplicationFrameHost.exe.
And it wont be the only window coming from that executable. So I'd recommend storing the calculator window's hwnd and then using it.

#!c::
#c::
    Process, Exist, % "Calculator.exe"  ;see if a calculator exists
    if (!ErrorLevel)                    ;ErrorLevel was set to 0 (false) if doesnt exist
    {
        Run, % "calc.exe"
        WinWait, % "Calculator"         ;make sure this is correct for your language
        hwnd := WinExist()              ;use last found window
        return
    }
    else if (!hwnd || A_ThisHotkey ~= "!") ;set or update hwnd      
        hwnd := WinActive("A")
    else
        WinActivate, % "ahk_id " hwnd
return

So a two hotkey solution, the other hotkey is just extra, it's not even needed unless you close and relaunch calculator and need to re-get its hwnd.
First we check if a calculator exists, if not, launch one, wait for it to open, store its hwnd and return.
Useful documentation links: Last found window and WinExist().

If a calculator did exist, check if we have a hwnd stored or if we used to alternative hotkey Win+Alt+C.
This is so if you have a calculator open, but don't have its hwnd stored. You can manually activate the calculator window and store its hwnd.
Useful documentation links: A_ThisHotkey and Regex match shorthand ~= (check if ! is found in A_ThisHotkey).

Finally, if nothing special needed doing, and just the basic Win+C hotkey was used, just active the calculator.

0
votes

Modifying your existing script (you don't need to overcomplicate it with getting the PID and ahk_class btw)


#c::
    BlockInput, On
    DetectHiddenWindows, on

    if not WinExist("Calculator")
    {
        Run calc.exe
        WinWait Calculator
        
    }

    WinActivate Calculator
    Winset, Alwaysontop, , A
    BlockInput, Off
    return