Jump to content
Sign in to follow this  
kcvinu

How to find last active window ?

Recommended Posts

Hi there,

I am planning to customize ALT TAB key combination. This is my pseudo code

 Get window list with WinList()

 If Mywindow1 and Mywindow2 is Exits Then

        Toggle that two windows.

 Else

        Find last active window and activate it.

So i need to know how to find the last active window.?


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

You would do something like so:

Local $myLastWin = WinActive("[ACTIVE]")
; your code goes after
Local $winList = WinList()
Local $Mywindow1 = "handle for window 1 here", $MyWindow2 = "handle for window 2 here"
If WinExists($Mywindow1) And WinExists($Mywindow2) Then
    ; toggle the windows
Else
    WinActivate($myLastWin)
EndIf

Does that help? :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

just send alt+tab as pressing alt+tab once takes you to the last active window.  But it would take more effort to find your script and run it than it would to just press alt+tab.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Hi MikahS, 

I didn't get the first line of your code. What is "[Active]" ?


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Hi 

boththose, Thanks but Send is not working

My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Hi MikahS, 

I didn't get the first line of your code. What is "[Active]" ?

[ACTIVE] is the window that is active right now. Storing the returned result (which is the handle) in a variable will give you the ability to use WinActivate to re-activate the window that you want.

EDIT: see the title special definiton

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

Hi 

MikahS

Thanks. Let me try


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Hi MikahS, 

It is partially working now. See my code. (Attached image). Code in line number 21 is not working. Rest is ok. 

post-89644-0-89174300-1421864011_thumb.p


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

The reason is probably due to if $Nuendo_hnd and $Word_hnd are both existing (or they are open windows), line 21 will never run. The only way you'll be able to get it to do line 21 is if Nuendo or MS word did not exist.

If you only had one window that exists like MS word then it would run, but not with both open.

EDIT: does that help clear it up?

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

send works fine

If not winexists("Microsoft Word") AND not winexists("nuendo") Then send("!{TAB}")

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

It is not working even when both "Nuendo" and "Word" is not running..


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

on line 10 use

WinGetTitle("[ACTIVE]")

instead of WinActive. I think it was a problem trying to use the handle instead of a title.

EDIT: Try that and post results. :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
It is not working even when both "Nuendo" and "Word" is not running..

 

 

Makes me think we are troubleshooting your If statement, not the line suggested.

Did you try running just the line I provided by itself?

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Can you try this ?

$hPreviousWindow = _WinGetNextWindow("[ACTIVE]" , "", 2)
ConsoleWrite("Previous Window : " & @CRLF & "HWND = " & $hPreviousWindow &  @CRLF & "Title = " &  WinGetTitle($hPreviousWindow) & @CRLF)

 
 
; #FUNCTION# ====================================================================================================================
; Name ..........: _WinGetNextWindow
; Description ...: Retrieves a handle to the next or previous visible window in the Z-Order.
; Syntax ........: _WinGetNextWindow($vWindow[, $sText = ""[, $iCmd = 0]])
; Parameters ....: $vWindow             - The title/hWnd/class of the window. See Title special definition.
;                  $sText               - [optional] The text of the window to check. Default is an empty string. See Text special definition.
;                  $iOrder              - [optional] Indicates whether the function returns a handle to the next window or the previous window.
;                                         This parameter can be either of the following values :
;                                          $GW_HWNDNEXT (2) : Returns a handle to the window below the given window.
;                                          $GW_HWNDPREV (3) : Returns a handle to the window above the given window.
; Return values .: Success - Returns a window handle
;                  Failure - Returns 0  and sets @error flag to non-zero on error
; Author ........: jguinch
; Remarks .......: If the specified window is a topmost window, the function searches for a topmost window.
;                  If the specified window is a top-level window, the function searches for a top-level window.
;                  If the specified window is a child window, the function searches for a child window.
; Link ..........:
; ===============================================================================================================================
Func _WinGetNextWindow($vWindow, $sText = "", $iCmd = 2)
    Local $hWnd = $vWindow, $aGetWindow
    If NOT IsHWnd($vWindow) Then $hWnd = WinGetHandle($vWindow, $sText)
    If $hWnd = 0 Then Return SetError(2, 0, 0)
 
    If NOT IsDeclared("GW_HWNDNEXT") Then Local Const $GW_HWNDNEXT = 2
    If NOT IsDeclared("GW_HWNDPREV") Then Local Const $GW_HWNDPREV = 3
 
    If $iCmd <> $GW_HWNDNEXT AND $iCmd <> $GW_HWNDPREV Then Return SetError(3, 0, 0)
 
    While 1
        $aGetWindow = DllCall("User32.dll", "hwnd", "GetWindow", "hwnd", $hWnd, "uint", $iCmd)
        If @error Then Return SetError(4, 0, 0)
        If $aGetWindow[0] = 0 Then ExitLoop
 
        $hWnd = $aGetWindow[0]
 
        $aGetWindowLong = DllCall("User32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", -16)
        If @error Then ExitLoop
 
        If NOT BitAND(WinGetState($hWnd), 2) OR $aGetWindowLong[0] < 0 Then ContinueLoop
 
        Return $hWnd
    WEnd
 
    Return SetError(1, 0, 0)
EndFunc
Edited by jguinch

Share this post


Link to post
Share on other sites

SmOke_N : your question is for me ? When I run my code, the Previous Window is How to find last active window ? - General Help and Support - AutoIt Forums - Mozilla Firefox :)

Share this post


Link to post
Share on other sites

Simplified with the Winapi UDF:

#include <WinAPI.au3>
 $hPriorActiveWindow = _WinAPI_GetWindow(WinGetHandle("[ACTIVE]"),$GW_HWNDNEXT)
 WinActivate($hPriorActiveWindow)
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Makes me think we are troubleshooting your If statement, not the line suggested.

Did you try running just the line I provided by itself?

No, The code provided by you is not working. 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

 

Simplified with the Winapi UDF:

#include <WinAPI.au3>
 $hPriorActiveWindow = _WinAPI_GetWindow(WinGetHandle("[ACTIVE]"),$GW_HWNDNEXT)
 WinActivate($hPriorActiveWindow)

Thank you, let me try


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Did you try post #12?

Glad you found a solution! ;)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • By ThePoro
      Hi everyone.
      I want to ask about this : 
      I want it runs from 1 to 100 and It opens 10 firefox profiles then access youtube. After I close a firefox window, the loop runs and wait for another window close until loop ends
      I have a loop like this.
      Func launch() Local $from = Int(GUICtrlRead($input1)) Local $to = Int(GUICtrlRead($input2)) If $to <> "" Then While $from <= $to Local $profile = $to _RunDos("start firefox.exe -p " & $profile & " -no-remote youtube.com") $to=$to+1 WEnd Else Local $profile = $to _RunDos("start firefox.exe -p " & $profile & " -no-remote youtube.com") EndIf EndFunc Is there any solution?
      Thank you!
    • By Rhidlor
      I was wondering if there was a library or something which provides the capability to Send() to inactive windows, and I know what you're thinking, I could just use ControlSend(); the reason I can't use that in this situation is because I need to hold down keys for specific prolonged periods of time. Also activating the window, Send()ing then de-activating the window isn't really an option here, I need the target window to always be in the background. I've looked around the forums for an adequate amount of time and didn't find anything useful, perhaps because the threads were all 10 years old, nevertheless, if anyone has any suggestions they would be greatly appreciated.
      Thanks!
    • By bbProg
      Hi,
       
      how can I wait activate for windows with information as in the photos attached?
      Class is not being recognized by the script.
       
      thanks in advanced.



    • By rcmaehl
      A UDF with Extended Functions for Window Management
       
      Notes:
      Fixes WinGetClassList's barbaric returning of a @LF separated string instead of an array.
       
      Potential Uses:
      Automating applications that change their controls' handles/classes on each launch (e.g. half of Cisco's programs)
       
      Functions:
      _WinGetClassList
      _WinGetClassNNList
      _WindowGetHandleList
      _WindowGetHandleListFromPos
       
      Download: 
      WindowEx.zip  (v0.4)
       
      Changelog:
      10/04/2016 (v0.4): _WinGetClassNNList Fixed : Not Returning an Index when using $2D_ARRAY _WinGetClassNNList Fixed : Not Properly returning $aArray[x][1] on Classes with instances > 9 when using $2D_ARRAY 10/03/2016 (v0.3): _WinGetClassList Added : Exactly the same as WinGetClassList but returns a more civilized Array _WinGetClassNNList Added : Returns Classes and their instances in either a 1D or 2D array depending on Flags _WindowGetHandleList Renamed: _WinGetHandleList SCRIPT BREAKING! _WindowGetHandleListFromPos Renamed: _WinGetHandleListFromPos SCRIPT BREAKING! 10/01/2016 (v0.2): WindowsExConstants.au3 Added : Flags in _WindowGetHandleListFromPos _WindowGetHandleListFromPos Removed: ConsoleWrite left in during debug _WindowGetHandleListFromPos Added : Flag for if part of a Control is at $X, $Y return it as well. 10/01/2016 (v0.1): _WindowGetHandleList Added : Retrieves the handles of classes from a window. _WindowGetHandleListFromPos Added : Retrieves the handles of classes at a specific position from a window. Known and Reported Bugs:
      None reported To Do:
      To Be Decided. Opinions welcome! Upcoming Changes:
      To Be Decided.
    • By FrancescoDiMuro
      Good morning everyone

      I was playing a little bit with "Screen Capture" UDF, and I was trying to make a "Window" capture, but, since I made a GUI which through I fire the event "Capture", my GUI is captured as well, and I don't want to
      This is the line of code that makes the capture:
       
      _ScreenCapture_CaptureWnd($strScreenCaptureFileName, $objActiveWindow, 0, 0, -1, -1, False) And these are the lines of code which select the "active" window:
       
      Local $objCurrentWindow = 9999 If _IsPressed("01") Then $objCurrentWindow = WinGetHandle("[ACTIVE]") If $objCurrentWindow <> $objMyGUI Then $objActiveWindow = $objCurrentWindow EndIf EndIf Sorry If I made stupid mistakes
      Thanks in advance.

      Francesco
×
×
  • Create New...