Jump to content

Recommended Posts

Posted (edited)

Hello everyone

I have a little script with which I switch between two windows, by using WinActivate (see attachment).  From other forum posts, I gather that "activate" and "get focus" is the same thing.

Unfortunately, whether the "activated" window also becomes gets the focus (i.e. its window bar's colour changes to the active window colour and shortcuts are sent to that window), depends on the program.  One of my programs, Akelpad, moves to the front but it doesn't take the focus when it moves to the front.  If I use the script with two Akelpad windows, neither window takes the focus when it moves to the front.  If I use an Akelpad window and e.g. a Word window, then Word takes the focus when Word moves to the front, but Akelpad does not take the focus when Akelpad moves to the front.  I'm not sure where the focus is... pressing Alt+F4 doesn't bring up the dialog to shut down Windows, so I conclude that the focus isn't on the taskbar or the desktop, but no other windows get the focus.  In fact, if I press Alt+F4 after Akelpad has been activated, it causes Akelpad to get the focus (and pressing it again closes Akelpad, as expected).

The short solution is "don't use Akelpad", but I don't know if this might affect other programs that I don't know of yet.  A temporary solution is to decide that whenever I encounter such a misbehaving window, I should simply snap the window to the top of the screen and then click with my mouse on the window bar each time the window gets activated.

Do you know of anything else I can try to ensure that the activated window also gets the focus?

Samuel

PS. I tried pasting the code, but the forum software makes a lot of the code "red" that shouldn't be red.

winswitchie.au3

Edited by leuce
Posted

Okay, FWIW, I discovered how to make it work with Akelpad, but I don't quite know why it works.  Of the two attached scripts, the "TITLE" version does not cause Akelpad to get the focus, but the "HANDLE" version does cause Akelpad to get the focus, even though the relevant portions of the scripts appear to be identical.  The only difference is that in the TITLE version, Akelpad is not the active window at the time that its handle is detected.

winswitchie HANDLE v01.au3 winswitchie TITLE v01.au3

Posted

I'd be careful using - AutoItSetOption ("WinTitleMatchMode", 2)

As Windows has a bunch of hidden windows, that can get the focus or be active I believe.
It may even be the case that a hidden instance of Akelpad exists.

You can check by using WinList.

Personally, I try to identify the correct Window and then get the handle, because sometimes a window title will change, but the handle doesn't.

Mode '1' (default) is safer, as you match the title starting at text from the left. The idea is to make your window title unique enough.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Posted (edited)
13 minutes ago, TheSaint said:

I'd be careful using - AutoItSetOption ("WinTitleMatchMode", 2)
As Windows has a bunch of hidden windows, that can get the focus or be active I believe.
It may even be the case that a hidden instance of Akelpad exists.
You can check by using WinList.

Thanks, I checked that out.  There are quite a few hidden windows, but there are no additional Akelpad windows.  Anyway, when I activate an Akelpad window, the Akelpad window does move to the front, so I'm not activating something else.

BTW, I tend to prefer WinTitleMatchMode = 2 generally because sometimes windows have very, very long titles, and sometimes the portion at the start of the title changes.  For example, sometimes the program's name is at the start of the title, so the user has to type the entire program name plus the unique part of the title.  Also, I have programs in which an unsaved file gets an asterisk in front of the title, so matching the title from the start only works when the file is not unsaved.

Anyway, in the second version of the script (uploaded as the "TITLE" version above) I changed it so that switching uses handles, even though the user identifies the windows using their titles initially. 

Edited by leuce
Posted
7 minutes ago, Zedna said:

Use ControlFocus()

Thanks, I'll investigate that.  Do you know of a generic control name that is "the window itself" or that refers to the current's window's window bar?  I checked a couple of windows' window bars, but they all have different control names.

Posted (edited)

As far as I know in ControlFocus() you can set ControlID parameter empty (string) and it finds "last active control in that window".

According to AutoIt sources (version 3.1.0) there is in such case internally used handle of window instead of empty handle of control in ControlID parameter.

Edited by Zedna
Posted

Strange enough, activating the main Edit controls directly seems to work:

Global $Win1_h, $Win1_ctrl
Global $Win2_h, $Win2_ctrl

$aWinlist = WinList("[CLASS:AkelPad4;]")
For $i = 1 To $aWinlist[0][0]

    if StringInStr($aWinlist[$i][0],"test1") then
        $Win1_h = $aWinlist[$i][1]
        $Win1_ctrl = ControlGetHandle($aWinlist[$i][1], "", "[CLASS:AkelEditW]")
    endif

    if StringInStr($aWinlist[$i][0],"test2") then
        $Win2_h = $aWinlist[$i][1]
        $Win2_ctrl = ControlGetHandle($aWinlist[$i][1], "", "[CLASS:AkelEditW]")
    endif

Next

Global $i = 1

HotKeySet("`", "switchie")
HotKeySet("!`", "helpie")
HotKeySet("^`", "exittie")

While 1
    Sleep("100")
WEnd

Func exittie()
    Exit
EndFunc   ;==>exittie

Func helpie()
    MsgBox(0, "", "[" & $Win1_h & "]" & @CRLF & "[" & $Win2_h & "]", 0)
EndFunc   ;==>helpie

Func switchie()

    If $i = 1 Then
        $i = 2
    Else
        $i = 1
    EndIf

    If $i = 1 Then
        ; WinActivate($Win1_h)
        WinActivate($Win1_ctrl)
        ; WinActivate($Win1_h)
    Else
        ; WinActivate($Win2_h)
        WinActivate($Win2_ctrl)
        ; WinActivate($Win2_h)
    EndIf

    ConsoleWrite(WinGetTitle(WinGetHandle("[ACTIVE]", "")) & @CRLF)

EndFunc   ;==>switchie

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...