Jump to content

What window had focus?


timehoard
 Share

Recommended Posts

Consider a tray menu or a floating button bar that is supposed to act on the current window, that is, on whatever window had focus just before the menu or button bar stole it. Is there a way in AutoIt to return focus to that window?

Functional examples would be the tray menus of the programs PhraseExpress and Ditto. Select canned text from one of these menus and it is instantly inserted in the foreground window. A dysfunctional example is this:
 

Opt("TrayMenuMode", 3)   ; no default items, no checkmarks

Opt("TrayOnEventMode", 1)



TrayCreateItem("Type Stuff")

TrayItemSetOnEvent(-1, "SendText")

TrayCreateItem("") ; Separator line

TrayCreateItem("Exit")

TrayItemSetOnEvent(-1, "Quit")



While 1

    Sleep(250)

WEnd



Func SendText()

    Send("Hello")

EndFunc



Func Quit()

    Exit

EndFunc
Link to comment
Share on other sites

  • Moderators

timehoard,

WinGetHandle("[ACTIVE]") returns the handle of the current active window. You could perhaps poll that using an Adlib function so that you always have the last active window to which you can then return with WinActivate. But do remember to deregister the Adlib function while the tray menu is open or you will get that handle instead. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

something simple example wise:

Opt("TrayMenuMode", 3)   ; no default items, no checkmarks

Opt("TrayOnEventMode", 1)

Local $currentWin

AdlibRegister("getCurrentWin")

TrayCreateItem("Type Stuff")

TrayItemSetOnEvent(-1, "SendText")

TrayCreateItem("") ; Separator line

TrayCreateItem("Exit")

TrayItemSetOnEvent(-1, "Quit")

While 1
    
    Sleep(250)

WEnd

Func getCurrentWin()
$currentWin = WinGetHandle("[ACTIVE]")
EndFunc


Func SendText()
    AdlibUnRegister("getCurrentWin")
    Send("Hello")
    WinActivate($currentWin)
    AdlibRegister("getCurrentWin")
EndFunc

Func Quit()

    Exit

EndFunc

EDIT: Used Melba23's solution to give an example, Adlib was the way to go on this problem ;)

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

 

Link to comment
Share on other sites

Also, welcome to the AutoIt forums! ;)

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

 

Link to comment
Share on other sites

Thanks, guys. I will gratefully go with one of those. I had looked at the WinGetHandle solution myself and provisionally rejected it because of the possibility that focus could change between the time WinGetHandle is polled and action is taken.

 

If you feel like it is not waiting enough time for you to switch between then you can bump up the adlibRegister 2nd param ms value to something like 1000 = 1 second. It has a default value of 250 ms which is pretty fast.

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

 

Link to comment
Share on other sites

No, after you two forced me to think about WinGetHandle, I realized that my initial worries were goofy and illogical. The little test script appears to be performing flawlessly.
 

Opt("TrayMenuMode", 3)   ; no default items, no checkmarks
Opt("TrayOnEventMode", 1)

Global $hCurrentWin   ; was Local

AdlibRegister("GetCurrentWin", 500)

TrayCreateItem("Type Stuff")
TrayItemSetOnEvent(-1, "SendText")
TrayCreateItem("") ; Separator line
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "Quit")

While 1
    Sleep(1000)
WEnd

Func GetCurrentWin()
    $hCurrentWin = WinGetHandle("[ACTIVE]")
EndFunc

Func SendText()
    AdlibUnRegister("GetCurrentWin")
    WinActivate($hCurrentWin)
    Send("Hello")
    AdlibRegister("GetCurrentWin", 500)
EndFunc

Func Quit()
    Exit
EndFunc
Edited by timehoard
Link to comment
Share on other sites

Use controlsettext, and then you don't need to worry about the window being active at all (generally).

controlsettext
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.
Link to comment
Share on other sites

 

Use controlsettext, and then you don't need to worry about the window being active at all (generally).

controlsettext

(1) ControlSetText requires a window as its first parameter. The issue in this thread is knowing which window.

(2) Text was merely an example. The method needs to apply at any operation on a window.

 

Edited by timehoard
Link to comment
Share on other sites

 

No, after you two forced me to think about WinGetHandle, I realized that my initial worries were goofy and illogical. The little test script appears to be performing flawlessly.

 

Opt("TrayMenuMode", 3)   ; no default items, no checkmarks
Opt("TrayOnEventMode", 1)

Global $hCurrentWin   ; was Local

AdlibRegister("GetCurrentWin", 500)

TrayCreateItem("Type Stuff")
TrayItemSetOnEvent(-1, "SendText")
TrayCreateItem("") ; Separator line
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "Quit")

While 1
    Sleep(1000)
WEnd

Func GetCurrentWin()
    $hCurrentWin = WinGetHandle("[ACTIVE]")
EndFunc

Func SendText()
    AdlibUnRegister("GetCurrentWin")
    WinActivate($hCurrentWin)
    Send("Hello")
    AdlibRegister("GetCurrentWin", 500)
EndFunc

Func Quit()
    Exit
EndFunc

 

Glad to hear it ;)

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

 

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