Autohotkey fails to open a program that Windows 10 lists as a background process

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

Posted 2016-08-24T16:17:28.697

Reputation: 8 059

I've had similar problems with AutoHotKey in Windows 10. I made a basic script for my wife to toggle her touch screen on or off, and it works great at first, but after a few uses it becomes intermittent. Haven't had a chance to figure out why yet, but I never had this sort of problem with AutoHotKey in older versions of Windows. – freginold – 2017-08-09T15:08:52.597

Answers

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

Posted 2016-08-24T16:17:28.697

Reputation: 1 341

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
    }
}

Michael Askin

Posted 2016-08-24T16:17:28.697

Reputation: 11

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.

Posted 2016-08-24T16:17:28.697

Reputation: 1 394

1That other question was helpful but ultimately not effective, since Calculator is not included in the Background Apps options and they had no other suggestions besides "use a different program". – techturtle – 2016-08-24T19:01:09.260

My other idea is use autohotkey in someway. When the calculator is open, it moves up to the apps portion in task manager yes? Maybe you can modify your script to first kill the calculator background process and then reopen it. Not sure if it's possible to differentiate between a background process and an active application in autohotkey though. – Tim G. – 2016-08-24T19:09:41.900

Also, my calculator is included in the background apps section. I can toggle it on and off. – Tim G. – 2016-08-24T19:48:23.483

1This background process stuff is all messed up. On my main desktop at home it is almost always in the background. On my laptop it is rarely in the background but that one has it is the "background apps" section while none of my other PCs have it. I thought maybe it was the Win10 Anniversary Update, because the laptop has it but the desktop doesn't, but I just upped the desktop and it didn't get it. – techturtle – 2016-08-25T12:33:29.257

As far as using AHK to check if the background process was running, that is one of the options I was hoping someone could tell me here. I have been unable to find a way to distinguish between background or not. I could probably just kill any calculator windows it finds and then open a new one, but I'd prefer not to do that because #1 if I'm using the calculator a lot I may want the value to be retained, and #2 killing it may just shove it in the background which would ultimately not solve the problem. – techturtle – 2016-08-25T12:39:09.647

1A couple of Windows Updates later and the calculator option appeared in Background Apps on both my computers. Unfortunately, the process is still appearing in the Background Processes section of Task Manager despite this setting, so it appears that finding a way to distinguish active vs background processes will be the only way to fix this. I've still had no luck on those grounds yet... – techturtle – 2016-08-30T11:48:12.263

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).

MarkPhil

Posted 2016-08-24T16:17:28.697

Reputation: 26