Jump to content

WinActivate($hwnd2) activates but not focuses background window


Recommended Posts

#include <WinAPISys.au3>
#include <WinAPISysWin.au3>

#include <WinAPISys.au3>
#include <WinAPIvkeysConstants.au3>

#include <misc.au3>

HotKeySet ( "#y" , "temp" )
While 1
        Sleep(100)
WEnd


Func temp()

    $hwnd1 = WinGetHandle("[ACTIVE]")
    Sleep(300)
    $hwnd2 = _WinAPI_GetWindow ($hwnd1, $GW_HWNDNEXT   )
    Sleep(300)
    WinActivate($hwnd2)
    Sleep(300)
    Send ("win2")
    Sleep(100)
    WinActivate($hwnd1)
    Send ("original win ")

EndFunc

What the above is supposed to : Activate next window according to 'window order' / alt tab order. Send keys.  Activate original window. Send keys

FOr this I get the hwnd of current window and next window with wingethandle and _WinAPI_GetWindow respectively.

Attempting to activate and focus second window with: WinActivate($hwnd2), doesn't work completely. The window does get activated , but doesn't have focus. As in, some windows like notepad2 are able to receive Ctrl+v , but not other keys. Most windows are not able to receive any keys.

To replicate , first window can be anything - Notepad, firefox with address bar having focus. Second window: scite editor, notepad2.

I do not understand what's wrong here . I think _WinAPI_GetWindow doesn't get a 'proper' hwnd of the background window ?

 

Any ideas please ? Thanks

PS: All I want to do is alt+tab . Simple send keys was being unreliable for me.

 

Link to comment
Share on other sites

Have you tried using WinActive() in case the send command is being sent before the window actually becomes active?

Also, _WinAPI_GetWindow ($hwnd1, $GW_HWNDNEXT) will return as explained in Function Reference -> " $GW_HWNDNEXT - The retrieved handle identifies the window below the specified window in the Z order.
        If the specified window is a topmost window, the handle identifies the topmost window below the specified window.
        If the specified window is a top-level window, the handle identifies the top-level window below the specified window.
        If the specified window is a child window the handle identifies the sibling window below the specified window. "

So it is not always the next window...

Edited by AoRaToS

s!mpL3 LAN Messenger

Current version 2.9.9.1 [04/07/2019]

s!mpL3 LAN Messenger.zip

s!mpL3

Link to comment
Share on other sites

Maybe something like this works?

Global $count = 0

HotKeySet ("+{F7}" , "temp")

While 1
    Sleep(100)
WEnd

Func temp()
    $count = $count + 1
    $WindowList = WinList()
    Local $NewWindowList[100][10]
    $newcount = 1
    For $i = 1 To $WindowList[0][0]
        If $WindowList[$i][0] <> "" And BitAND(WinGetState($WindowList[$i][1]), 2) Then
            $NewWindowList[0][0] = $newcount
             $NewWindowList[$newcount][0] = ($WindowList[$i][0])
            $NewWindowList[$newcount][1] = ($WindowList[$i][1])
            $newcount = $newcount + 1
        EndIf
    Next
    If $count > $NewWindowList[0][0] then $count = 1
    WinActivate($NewWindowList[$count][1])
EndFunc

 

Edited by AoRaToS

s!mpL3 LAN Messenger

Current version 2.9.9.1 [04/07/2019]

s!mpL3 LAN Messenger.zip

s!mpL3

Link to comment
Share on other sites

Perfect! you are a life saver man/woman.

I fine tuned your function to toggle between 2 windows and eliminate focusing the desktop

Func temp1()
    ;$count = $count + 1
    $count = 2
    $WindowList = WinList()

    Local $NewWindowList[100][10]
    $newcount = 1
    For $i = 1 To $WindowList[0][0]
        If $WindowList[$i][0] <> "" And $WindowList[$i][0] <> "Program Manager" And BitAND(WinGetState($WindowList[$i][1]), 2) Then
            $NewWindowList[0][0] = $newcount
             $NewWindowList[$newcount][0] = ($WindowList[$i][0])
            $NewWindowList[$newcount][1] = ($WindowList[$i][1])
            $newcount = $newcount + 1
        EndIf
    Next

    If $count > $NewWindowList[0][0] then $count = 1
        WinActivate($NewWindowList[$count][0])
EndFunc

Next order of business is to emulate alt+tab further. So +F7 F7 focuses the third window. Like alt+tab tab focuses the third window. Alt+tab tab tab focuses 4th window and so on ..

Any ideas on how to go about it ?

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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