timehoard Posted September 29, 2014 Share Posted September 29, 2014 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: expandcollapse popupOpt("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 More sharing options...
Moderators Melba23 Posted September 29, 2014 Moderators Share Posted September 29, 2014 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
MikahS Posted September 29, 2014 Share Posted September 29, 2014 (edited) something simple example wise: expandcollapse popupOpt("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 September 29, 2014 by MikahS Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy 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 More sharing options...
timehoard Posted September 29, 2014 Author Share Posted September 29, 2014 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. Link to comment Share on other sites More sharing options...
MikahS Posted September 29, 2014 Share Posted September 29, 2014 Also, welcome to the AutoIt forums! Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy 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 More sharing options...
timehoard Posted September 29, 2014 Author Share Posted September 29, 2014 Also, welcome to the AutoIt forums! Thank you. MikahS 1 Link to comment Share on other sites More sharing options...
MikahS Posted September 29, 2014 Share Posted September 29, 2014 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 ~ getENVvarsMy 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 More sharing options...
timehoard Posted September 29, 2014 Author Share Posted September 29, 2014 (edited) 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 September 29, 2014 by timehoard Link to comment Share on other sites More sharing options...
jdelaney Posted September 29, 2014 Share Posted September 29, 2014 (edited) Use controlsettext, and then you don't need to worry about the window being active at all (generally). controlsettext Edited September 29, 2014 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 More sharing options...
timehoard Posted September 29, 2014 Author Share Posted September 29, 2014 (edited) 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 September 29, 2014 by timehoard Link to comment Share on other sites More sharing options...
junkew Posted September 29, 2014 Share Posted September 29, 2014 example 19 - 24 can help '?do=embed' frameborder='0' data-embedContent>> /page-6#entry1143298'>/page-6#entry1143298 FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
MikahS Posted September 30, 2014 Share Posted September 30, 2014 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 ~ getENVvarsMy 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now