1
votes

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.

In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box

Below is the simple code for testing:

F10::

    SetTitleMatchMode, 1
    DetectHiddenWindows, Off

    WinGetTitle, startTitle, A
    msgbox % "Start Title = <" . startTitle . ">"

    ;WinActivate, startTitle

    inputbox, mode, Test box,  Testing,,260,160
    sleep 500
    WinActivate,  startTitle

Return

This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.

Can someone please explain to me what is going on with WinActivate?

I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.

1

1 Answers

1
votes

You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).

Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.

WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.

And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.

Here's your revised code:

F10::
    _HWND := WinExist("A")
    MsgBox, % "Start hwnd = <" _HWND ">"
    InputBox, mode, Test box,  Testing,,260,160
    Sleep, 500
    WinActivate, % "ahk_id " _HWND
Return