Jump to content

Clicking On Message Box Takes Focus Away?


Go to solution Solved by SmOke_N,

Recommended Posts

Hello,
 
This is my first autoIT script.. 
 
I have a script that goes back and forth between MS Excel and a Terminal Window. Copy's data from excel and then pastes it into the terminal window... goes back to Excel grabs a new set of data, comes back to terminal and pastes the data.. Loops until finished. After each copy and paste I have a message box prompt the user if they would like to continue. The below is working fine and I have no issues. However It only works if I Activate the excel window twice.. If I activate the excel window only once.. and then click ok on the message box to continue it looses focus and stays on the terminal window.. Does not appear to go back to excel. I broke the Window Activator into a function and noticed if I activate window before the message box, and then activate the window after this works.. I don't know why I cant activate the terminal, then prompt the message box, and then activate excel again.. Having trouble understanding why the second activation is required...
 
 
Func Start()
While 1
   ;Window Checker Function
   $excel_check = Window_Checker($excel_title)
   $terminal_title = Window_Checker($martens_title)
   ;Check if Excel and Note Pad Are Running;If running execute script;If not running exit script
   if $excel_check == True and $terminal_title == True Then
Activator($excel_title)
$sData = Excel_Copy() ;Copy From Excel Function
Terminal_AC_Clear() ;Clear AC Window Function
Activator($excel_title)
$check = MsgBox($MB_OKCANCEL, "", "Do you want to continue?")
if ($check == 2) Then
MsgBox("", "", "BREAKING")
Exit
EndIf
EndIf
WEnd
EndFunc
 
;FUNCTIONS;Activates a Window
Func Activator($window)
WinActivate($window)
WinWaitActive($window)
EndFunc
 
Func Terimal_AC_Clear()
 Activator($terminal_title)
 Send("{HOME}")
 ;Clear Selection Box by pressing ALT+END
  Send("!{END}")
EndFunc
 
Func Window_Checker($title)
   Local $exists = False
   If WinExists($title) Then
 $exists = True
   Else
 $exists = False
   EndIf
   Return $exists
EndFunc

 

Link to comment
Share on other sites

  • Moderators
  • Solution

Being active doesn't always ensure keyboard focus.

You could try to replace you Activator function with:

;FUNCTIONS;Activates a Window
Func Activator($window)
    ; if both are true, loop code doesn't execute
    While WinExists($window) And Not WinActive($window)
        WinActivate($window)
        Sleep(10) ; small sleep so we don't overload cpu
    WEnd
    If Not WinExists($window) Then
        ; window no longer exists
        ; could do error checking if you wanted
        Return SetError(1, 0, 0) 
    EndIf
    ; success, window exists and is active
    Return 1
EndFunc   ;==>Activator

To see if that helps.

Or take it a step further with something like (Note the include file that would need to be included at the top of your script):

#include <WinAPI.au3>


;FUNCTIONS;Activates a Window
Func Activator($window)

    Local $h_wnd = $window
    If Not IsHWnd($h_wnd) Then
        If WinExists($h_wnd) Then
            $h_wnd = WinGetHandle($window)
        Else
            ; assume string/integer window handle passed
            $h_wnd = HWnd($h_wnd)
        EndIf
    EndIf

    ; if both are true, loop code doesn't execute
    While Not WinActive($h_wnd) And WinExists($h_wnd)
        WinActivate($h_wnd)
        Sleep(10) ; small sleep so we don't overload cpu
    WEnd
    If Not WinExists($window) Then
        ; window no longer exists
        ; could do error checking if you wanted
        Return SetError(1, 0, 0)
    EndIf

    ; ensure "keyboard" focus
    Local $h_oldfocus = _WinAPI_SetFocus($h_wnd)
    If @error Then
        ; see _WinAPI_SetFocus to understand "GetLastError"
        Return SetError(2, _WinAPI_GetLastError(), $h_oldfocus)
    EndIf

    ; success, window exists and is active
    ; return the windows hwnd which had focus before
    Return $h_oldfocus
EndFunc   ;==>Activator

Hopefully the comments help.

I believe the mystery you're running into, may be the z-order that windows set for the windows themselves upon creation.

.....

On a different note, you could probably benefit very much from learning the objects for excel and control functions for windows.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks a lot for the reply. I have incorporated the first function into my script and have had the script complete 100% successful so far. You're function is a lot cleaner than my sketchy one :D I also like the direction the 2nd function is headed but have not had a chance to play around. I also believe you are correct about the z-order.

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