3

I remapped my NumLock key using AutoHotKey so that it would open the calculator instead, using the following script:

NUMLOCK::
IfWinExist Calculator
{
    WinActivate Calculator
}
else
{
    Run, % "calc"
    WinWait Calculator
    WinActivate
}

This ran great up through Windows 7 (albeit with a different "run" line due to app name changes). The IfWinExist line checks to see if Calculator is already running. If it is, it activates it; if not, it launches it. This works to keep you from getting multiple copies of Calculator running in case you don't close it between uses.

With Windows 10 I have noticed that it only works intermittently. The first time I launch it following a reboot, everything works as normal. However, sometimes when closing the Calculator app, it saves it in the Background Processes, rather than truly exiting out.

enter image description here

When I activate the script, the IfWinExist will find it as running and (apparently) activates it but with no visible effect since it's a background process. If I open Task Manager and kill the process, normal function returns (for a while).

Is there any way to either prevent Windows from leaving the Calculator as a Background Process, or to make AutoHotKey distinguish between Apps and Background Processes?

techturtle
  • 9,376

5 Answers5

1

I don't use Windows 10 most of the time. It looks like you either need a way to detect that it's a background process and then either activate it (so it is no longer a background process) or just kill it and launch a new copy.

Short of actually figuring out how to activate the 'proper' way, one thing you could do would be to do a WinActivate followed by a WinWaitActive with a timeout. If ErrorLevel is set then it didn't activate properly (i.e., background process) and you could kill/re-launch.

You could also look at your current setting for A_DetectHiddenWindows and see if that's on--I'm guessing it's not on and is off by default. You could see if forcing DetectHiddenWindows, On causes any change in behavior (guessing not).

You could also try a WinShow after the IfWinExist returns true, i.e., before activating with WinActivate. I'm not sure what Windows 10 is doing behind the scenes but it may just be a hidden window that's present in the background, in which case you can show it. You can also find a utility called Spy++ or Spyxx that will show you all windows by thread, process or window with a tree view, which would show you if it has a hidden window present when it's running as a background process.

JJohnston2
  • 1,929
1

This is what I use. It's not the most beautiful code, but it is 100% reliable for Calculator on Windows 10, be it background process or not. I like marking it AlwaysOnTop based on my workflow; just remove that if you don't want it.

CalcOnTop = 0
;[Win+C] Calculator
*#c::
IfWinExist Calculator
{
    WinActivate Calculator
    WinWaitActive, Calculator, , 1
    if ErrorLevel
    {
        Process,WaitClose,calculator.exe,1
        CalcOnTop = 0
        Run calc.exe
        WinWait, Calculator, , 3
        if ErrorLevel
        {
            MsgBox, Error:  WinWait timed out. (3 seconds)
            return 
        }
        else
        {
            WinActivate Calculator
            Sleep 25
            WinSet, AlwaysOnTop, On, Calculator
            CalcOnTop = 1
            return
        }
        }
    if (CalcOnTop = 1)
        return
    else
    {
        WinSet, AlwaysOnTop, On, Calculator
        CalcOnTop = 1
        return
    }
}
else
{
    CalcOnTop = 0
    Run calc.exe
    WinWait, Calculator, , 3
    if ErrorLevel
    {
        MsgBox, Error:  WinWait timed out. (3 seconds)
        return 
    }
    else
    {
        WinActivate Calculator
        Sleep 25
        WinSet, AlwaysOnTop, On, Calculator
        CalcOnTop = 1
        return
    }
}
freginold
  • 630
0

I think this question will help you solve your problem. It looks like the reason it runs as a background process is for updates. You can disable this by going to Start -> Settings -> Privacy -> Background apps as the other question suggests.

Tim G.
  • 1,507
0

This is working for me. The only thing is you need to remember to close Calculator with a keyboard shortcut rather than with a mouse click (I use Ctrl+Shift+W, since I'm accustomed to using that for browsers). But WinClose seems to keep calculator from hanging around as a background process.

#IfWinNotExist Calculator
    NumLock::Run, Calc.exe
#IfWinNotExist

#IfWinExist, Calculator
    NumLock::WinActivate
#IfWinExist

#IfWinActive, Calculator
    ^+w::WinClose
#IfWinActive

If you really wanted to keep the option open of closing the window with a mouse click, I imagine you could set up something to also execute WinClose if a click is detected within a certain region of the active window (when the active window is Calc).

0

Calc showed up for me in Settings>Privacy>Backgound Apps.

After you untick the box allowing it to run as a background process you should be able to run it:

Win+R calc

then kill it with F4.

Once you do that the following script works fine from that point on for open/close:

SetNumLockState, AlwaysOn

NumLock:: IfWinExist, Calculator { IfWinActive, Calculator { WinClose, Calculator } else { WinActivate, Calculator } } else { Run calc WinActivate, Calculator } return

Xaneph
  • 1