Jump to content

Trouble using the current window with focus


toob
 Share

Go to solution Solved by toob,

Recommended Posts

Hello,

As part of my script, which is started via a windows keyboard shortcut, I wish to grab the content of the selected text of the window that currently has the focus.

These are the methods I have tried without sucess:

//Have tried with all four modes

Opt("WinTitleMatchMode", 4)
 

//This works when naming the window title, however I want to use any window that currently has the focus

//So for sake of simplicity I am using the same window for testing, but with the next line commented
;WinActivate("UltraEdit")

//$handle returns 0x00010066 but WinActivate($handle, "") fails to work, eg all subsequent command are not in the active window

Local $handle = WinGetHandle("[ACTIVE]")
MsgBox(0, "Test", "$handle=" & $handle)
WinActivate($handle, "")

//$localtitle always returns nothing, regardless of which of the four WinTitleMatchModes I set.
Local $localtitle = WinGetTitle("[ACTIVE]")
MsgBox(0, "Test", "$localtitle=" & $localtitle)
WinActivate($localtitle, "")

//This is what is meant to happen after I get the current window with focus

//which works if I explicitly name the window stitle

Send("{CTRLDOWN}")
;Sleep(100)
Send("c")
;Sleep(100)
Send("{CTRLUP}")
;Sleep(100)

$Var = ClipGet()

and so on...

Windows 7 64bit

Any ideas?

Edited by toob
Link to comment
Share on other sites

 

Can you explain why you need to call WinActivate when the window already has focus?

Edit: Have you tried this?:

$hWnd = WinGetHandle("[ACTIVE]")
$data = ControlCommand("", "", $hWnd, "GetSelected", "")
MsgBox(0, "Test", "$data =" & $data )

I'm having to call WinActivate because even though physically the window already has focus, autoit does not regonise this unless I name the window title. This is probably due to my minimized .bat window taking the focus sway. I use the .bat file to call autoit and the script:

"C:Program Files (x86)AutoIt3AutoIt3.exe" "C:UsersMarkDocumentsmarkAutoIt3 Scriptsclippaste.au3"

$data =0

Edited by toob
Link to comment
Share on other sites

Sounds like you have a unique environment. My suggestion would be to find a solution that works when the script is launched without the batch file. Once you have that, you can then reintroduce the batch file and then retest the script.

 

I have done away with the batch file so that the windows shortcut now does this:

 

"C:Program Files (x86)AutoIt3AutoIt3.exe" "C:UsersMarkDocumentsmarkAutoIt3 Scriptsclipappend.au3"

which is the same as the batch job.

I no longer see the dos window open however the focus is still stolen from the active window when I press the windows keyboard shortcut key combination to action the autoit script. The focus appears to be set to no other window whic is what is being reported by autoit.

Problem therefore is, how to call an autoit script yet maintian the focus of the current window so that the script can read the highlighted text of the active window prior to pressing the windows keyboard shortcut ?

Or is there another way to action starting an autoit script other than the windows keyboard shortcut, that maintains the or can set the focus back to the active window?

Link to comment
Share on other sites

Post a short script that demonstrates the issue. Without that, we can't duplicate / observe the problem and offer more direct advice on how to resolve it.

 

This is a very simple script, the pupose is to be able to append select text to the the windows clipboard rather than the default operation of overwritting clipboard contents.

 

The following script is given a windows shortcut, for example Ctl + Alt+ C

I place the windows shortcut generated by right clicking on the autoit script into:

C:UsersMarkAppDataRoamingMicrosoftWindowsStart Menu_hotkeys

So that they can be executed anywhere

Then modify the properties of the shortcut to include the path to the autoit exe

"C:Program Files (x86)AutoIt3AutoIt3.exe" "C:UsersMarkDocumentsmarkAutoIt3 Scriptsclipappend.au3"

To test, I have Ultredit or any editor I guess, open with a word highlighted ready for autoit to fetch it.

Using "WinActivate("UltraEdit")"  it works fine, but without it or any of the other methods the focus is lost.

You should see that the windows keyboard shortcut is somehow stealing the focus from the active ultraedit window. I know this isnt Autoit's fault, just looking for a solution, maybe looping open windows.

However please bare in mind that I would like this to work from any text editor including eclipse, ultraedit, .net etc.

;Opt("WinTitleMatchMode", 4)
 
;;WinActivate("UltraEdit")
;Local $handle = WinGetHandle("[ACTIVE]")
;MsgBox(0, "Test", "$handle=" & $handle)
;WinActivate($handle, "")

;Local $localtitle = WinGetTitle("[ACTIVE]")
;MsgBox(0, "Test", "$localtitle=" & $localtitle)
;WinActivate($localtitle, "")

;$hWnd = WinGetHandle("[ACTIVE]") 
;$data = ControlCommand("", "", $hWnd, "GetSelected", "") 
;MsgBox(0, "Test", "$data =" & $data )

$beforetext = ClipGet()

Send("{CTRLDOWN}")
Send("c")
Send("{CTRLUP}")

$newtext = ClipGet()


If NOT $newtext = "" Then
   If $beforetext = "" Then
      ClipPut($newtext)
   Else
      ClipPut($beforetext & @CRLF & $newtext)
   EndIf
EndIf

;;WinActivate("UltraEdit")

Thanks BTW for your help so far.

Link to comment
Share on other sites

It may be that the autoit hidden window steals focus when the script activates.

Might be worth a try to change it or minimize it.

After that, I think there was a script I seen which gets and sets the z-order of windows, if you can find that, or a way to do it, and get the highest window handle, then activate that maybe.

I have seen discussions on windows fighting for focus in the past.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Might be worth trying Winsetstate on autoit window with flag of 7 which is SW_SHOWMINNOACTIVE.

Or use the win API ShowWindow if the func does not accept that flag.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Solution

I've just looped the Z order and found that "Start" has the focus and my window is always 2nd. So I should be able to code a solution now. Thanks for everyones input.

UPDATE

Although not very elegant heres my solution, perhaps someone might offer up a cleaner version, like a break in the loop when window is found?

Local $windows = WinList()

$hWnd = 0;
$windowcount = 0

For $i = 1 To $windows[0][0]
    If $windows[$i][0] <> "" And IsVisible($windows[$i][1]) Then
        $windowcount = $windowcount + 1
        If  $windowcount = 2 Then
           $hWnd = $windows[$i][1];
                   ;BREAK
        EndIf
    EndIf
Next

Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
    Else
        Return 0
    EndIf

EndFunc   ;==>IsVisible

WinActivate($hWnd, "")
Edited by toob
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...