![]() |
KnowBrainer Speech Recognition | ![]() |
Topic Title: Bring window to front Topic Summary: A Script helper which forces a named window to the front Created On: 04/05/2021 02:08 PM Status: Post and Reply |
|
![]() |
![]() |
- Edgar | - 04/05/2021 02:08 PM |
![]() |
![]() |
- Edgar | - 04/05/2021 04:36 PM |
![]() |
![]() |
- Edgar | - 04/05/2021 04:38 PM |
![]() |
![]() |
- Edgar | - 04/05/2021 05:45 PM |
![]() |
![]() |
- kkkwj | - 04/06/2021 01:06 AM |
![]() |
![]() |
- Edgar | - 04/06/2021 10:10 AM |
![]() |
![]() |
- kkkwj | - 04/06/2021 02:44 PM |
![]() |
![]() |
- Edgar | - 04/06/2021 02:59 PM |
![]() |
![]() |
- PG LTU | - 04/06/2021 04:07 PM |
![]() |
![]() |
- kkkwj | - 04/06/2021 04:15 PM |
![]() |
![]() |
- Edgar | - 04/06/2021 05:47 PM |
![]() |
![]() |
- alexander | - 04/10/2021 09:34 PM |
![]() |
![]() |
- R. Wilke | - 04/11/2021 01:06 PM |
![]() |
![]() |
- quintijn | - 04/13/2021 11:09 AM |
![]() |
![]() |
- alexander | - 04/13/2021 01:04 PM |
![]() |
![]() |
- R. Wilke | - 04/13/2021 06:10 PM |
![]() |
![]() |
- alexander | - 04/15/2021 04:26 PM |
![]() |
![]() |
- kkkwj | - 04/14/2021 02:11 AM |
![]() |
![]() |
- R. Wilke | - 04/14/2021 11:00 AM |
![]() |
![]() |
- kkkwj | - 04/14/2021 11:00 PM |
![]() |
![]() |
- quintijn | - 04/20/2021 12:07 PM |
![]() |
![]() |
- kkkwj | - 04/20/2021 02:28 PM |
![]() |
![]() |
- Edgar | - 04/20/2021 02:44 PM |
![]() |
![]() |
- BigTech | - 04/20/2021 11:27 PM |
![]() |
![]() |
- BigTech | - 04/22/2021 11:13 PM |
![]() |
![]() |
- quintijn | - 04/28/2021 10:48 AM |
![]() |
![]() |
- BigTech | - 05/03/2021 11:04 PM |
![]() |
![]() |
- kkkwj | - 05/18/2021 03:01 PM |
![]() |
![]() |
- BigTech | - 12/11/2021 05:22 PM |
![]() |
![]() |
- R. Wilke | - 05/18/2021 04:05 PM |
![]() |
![]() |
- dilligence | - 05/19/2021 10:25 AM |
![]() |
![]() |
- R. Wilke | - 05/19/2021 03:19 PM |
![]() |
![]() |
- dilligence | - 05/20/2021 11:49 AM |
![]() |
![]() |
- alexander | - 06/17/2021 11:05 AM |
![]() |
|
Tiny improvement - allow multiple window titles… Only the Main method has changed:
static void Main(string[] args) { if (args.Length == 0) { MessageBox.Show("You must supply a window title", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string inputParameter = string.Join(" ", args).Trim(); string[] titles = inputParameter.Split(';'); List<string> windowTitles = new List<string>(); foreach (string phrase in titles) windowTitles.Add(phrase.Trim()); foreach (string phrase in windowTitles)//DEBUG efm5 2021 04 5 testing MessageBox.Show(phrase, "DEBUG TITLE", MessageBoxButtons.OK, MessageBoxIcon.Error);
Process[] processlist = Process.GetProcesses(); foreach (Process process in processlist) { if (!String.IsNullOrEmpty(process.MainWindowTitle)) { foreach (string phrase in windowTitles){ if (StringContainsPhrase(phrase, process.MainWindowTitle)) { NativeMethods.ShowWindowAsync(process.MainWindowHandle, NativeMethods.ShowWindowCommands.SW_SHOWDEFAULT); NativeMethods.ShowWindowAsync(process.MainWindowHandle, NativeMethods.ShowWindowCommands.SW_SHOW); NativeMethods.SetForegroundWindow(process.MainWindowHandle); } } } } } WARNING - although it works reliably every time on my system, my beta tester says it only works about half the time on theirs! ------------------------- -Edgar |
|
|
|
![]() |
|
Oh yeah, you use a semicolon to separate the window titles: ShellExecute "D:\\GetMousePosition\\GetMousePosition\\bin\\Release\\GetMousePosition.exe" & " " & _Arg1, 7 Wait 500
ShellExecute "D:\\BringWindowToFront\\BringWindowToFront\\bin\\Release\\BringWindowToFront.exe" & " " & "Mouse Coordinates; Preferences" I checked the syntax for "ShellExecute" and am wondering if that trailing ", 7" might be causing grief. ------------------------- -Edgar |
|
|
|
![]() |
|
It turns out that it looks like my beta tester was using the code after having modified it and trying to use it for something for which it was not intended. ------------------------- -Edgar |
|
|
|
![]() |
|
Hi Edgar, that was interesting! I've never seen ShowWindowAsync before. Does it bring the window to the front AND make it the active foreground window that gets keyboard input, etc? It seems like a really nice way to bring windows to the foreground even when your app didn't spawn the windows in the first place. Thank you for posting this example!
------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
That is my understanding. I think that there is a similar ShowWindow library function (no Asynchronous) but I am not sure what the differences are. From my experiences it looks like SetForegroundWindow can give a window focus but does not always make it display on the desktop; ShowWindowAsync seems to (almost?) always make it show on the desktop and, from my experience it always gets focus too. ------------------------- -Edgar |
|
|
|
![]() |
|
Well, I can vouch for the fact that calling ShowWindow from some random non-foreground piece of code rarely if ever works reliably. Windows has rules against doing that. But it looks like you have figured out a clever way of just sending a message to tell the window to take over the foreground itself. I'll have to try it out!
------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
|
|
|
|
![]() |
|
I hope I haven't inadvertently created a problem. Whenever I use ShellExecute to send stuff to an AHK program, I use "6" at the end ("minimized window; not active") because I don't need to see anything (and I have suggested such a thing over this forum often). Naturally, in a DVC (or DAS) that works on a window, I would use the proper windowStyle constant for the situation. -------------------------
|
|
|
|
![]() |
|
Hi Edgar, I'm with PG on the 6,7 thing. I was probably the "someone on the forum" who suggested using 7 was the right thing to do for times when I didn't want to see a DOS console window pop up when I ran a DOS exe file (your StatementConstructor, as I recall). As far as I could tell, there was no difference between 6 and 7. PG uses 6, and ended up with using 7 because it was the last one I tried and easiest to remember. I don't recall seeing the 10 before; I've certainly never tried it.
------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
I see that the Dragon® documentation in the Windows documentation (which I quoted) do not exactly agree. Dragon® says:
windowStyle Controls how the window appears when you activate the application. Must be one of the following: 1, 5, or 9 default window size; active (default) 2 minimized window; active 3 maximized window; active 4 or 8 default window size; not active 6 or 7 minimized window; not active but that may no longer be true with the new Basic in 15.61. ------------------------- -Edgar |
|
|
|
![]() |
|
fwiw, here is Mark's vocola extension file for doing window activation
https://github.com/mdbridge/bit-bucket/blob/c55b19563b11766350726f346b29ac61bcdfc266/voice/Vocola_development/unofficial/vocola_ext_window_switching.py#L226 Window.Go is great cause you can use regex. But win32gui.SetForegroundWindow is annoyingly unreliable (blinking app in taskbar but no activation) Most reliable thing I've found is to "Ctrl+click" the taskbar app. I have scripts to activate apps that way, never fails. After that, the SetForegroundWindow for that app can also work reliably in my experience. |
|
|
|
![]() |
|
-------------------------
|
|
|
|
![]() |
|
I prefer AutoHotkey for bringing windows to the foreground. Described on https://qh.antenna.nl/unimacro/features/autohotkey/index.html, and used a lot in Unimacro and Vocola.
The clue is, you have to write a tiny script with the handle of the window you want to bring into the foreground, and run that script with autohotkey.exe. I use it also the bring for example the Messages window of Natlink macros to the foreground, by the script with contents: ``` SetTitleMatchMode, 1 WinActivate Messages from ``` I bit of setup work, but worth the trouble IMHO, Quintijn |
|
|
|
![]() |
|
Do you by chance have a link to the deleted comments in that Microsoft post that show you how to override the "AllowForegroundWindow"?
![]() "Update (02/21): Deleted all comments that showed ways of circumventing the rules" Quintijin, Does autohotkey work completely reliably for you? I always hesitate to add yet another layer of technology to my stack... I used to use autohotkey, but I made the decision a while back to try to stick to purely Vocola and Python extensions if possible. (Although I do have to use the occasional .bat and .vbs, but since those come with Windows preinstalled they don't require any extra set up) |
|
|
|
![]() |
|
"Update (02/21): Deleted all comments that showed ways of circumventing the rules" No, and if I had, I wouldn't share them. I have had my fill fighting with SetForegroundWindow and trying to solve it with AllowForegroundWindow, to no avail. But then I read on and found Raymond showing a way that works, without circumventing the rules. As far as AutoHotKey, which I have never used and am not considering, doesn't it also fall back on the Win32 API after all? -------------------------
|
|
|
|
![]() |
|
When I read his post, it seems like he pretty much says there will always be a case when you can't do it, unless I misunderstand what he says or unless he's joking and being snarky:
In fact from what he says I'm surprised that it ever works at all. Given that we are running this from some background python process, presumably inside of dragons process space(?), A process that rarely if at all has the focus, how would it ever work?
Anyway searching around I found some stack overflow items but they come with warnings among other things.
I can of course understand the reason the OS is structured this way, but it really sucks for accessible applications as opposed to just malevolent applications...
In any case I will probably try some of those stack overflow suggestions at some stage, but my current hack seems to work well enough. |
|
|
|
![]() |
|
RW, I read Chen's paper long ago, plus many others. If there was something in there that showed the way, I must have missed it. I even tracked down and watched his conference presentation on Windows and foreground and window owners and parents and a chunk of code that was supposed to "pick the right child window" out of a hierarchy of open windows. Despite tons of study, I never got anything out of his window-climbing code. I even interacted with him and he pointed me toward Microsoft's UI Automation technology. Not much help. If you know how (within the rules) to bring an app window to the foreground from a background thread that is not the foreground thread that has love to give away, I'm sure many people would like to know...
------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
Oh dear, Kevin. I would have been surprised if Raymond had given you a straightforward answer along the lines of "just give me the code to copy and paste", he is certainly a lot more funny than that. However, his blog also has a superbe search function, and if you copy and paste "foreground activation love" into it, and press enter, you might find it. Probably at the top of the list. -------------------------
|
|
|
|
![]() |
|
Hi RW, I have had his paper, and his blog, and N comments about that article, and so on, for years. If you mean that the solution is at the top of his love article, I'll have another look at it. His best answer, given directly to me when I asked him directly about my voice application issue, was to consider the UIA (UI automation) interface. Ugh. That wasn't helpful at all. Oh well.
------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
autohotkey seems to be pretty reliable, but you also have to learn it, as each interface.
Greetings, Quintijn |
|
|
|
![]() |
|
Hi Quintijn, have you seen the thread on how experts use Dragon? I think it would be nice if some of your Natlink/Python crew made a video or two on how experts in your community use Dragon. Not many of us here know what a Python/Vocola/Unimacro/Dragonfly/Natlink setup looks like or feels like. I thought I would invite you. Thanks, Kevin
------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
+1 ------------------------- -Edgar |
|
|
|
![]() |
|
You must use the alt key for programmatically switching windows. Here's my code (ignore my variable casing, I'm not a programmer). Also don't see how to insert code in this post... EnumWindows(EnumWinHandle,NULL); //FND-NRM-ACT, press alt key ZeroMemory(SI,sizeof(INPUT)); SI[0].type=INPUT_KEYBOARD; SI[0].ki.wVk=VK_MENU; SI[0].ki.dwFlags=0; SendInput(1,SI,sizeof(INPUT)); if(IsIconic(WndoCurrent)) {//FND-NRM-ACT, restore destination window if minimized OpenIcon((WndoCurrent)); }//FND-NRM-ACT, restore destination window if minimized else {//FND-NRM-ACT, switch to destination window SetForegroundWindow(WndoCurrent); }//FND-NRM-ACT, switch to destination window //FND-NRM-ACT, release alt key SI[0].ki.dwFlags=KEYEVENTF_KEYUP; SendInput(1,SI,sizeof(INPUT)); GetTitle=false; *KeyString='\0'; |
|
|
|
![]() |
|
I posted that to the now defunct Yahoo forum many years ago. Quintijn, Mark, and maybe Rick were all present. Seems nobody picked up on it? Anybody home???
|
|
|
|
![]() |
|
I have not been on the Yahoo forum for many years...
Quintijn |
|
|
|
![]() |
|
Come to think of it... Holding down Alt while programmatically switching windows probably isn't a trick. In fact, we manually switch windows in Windows by holding the Alt key while tapping Tab. The reason holding Alt is required for programmatic window switching might be related to that normal UI operation.
|
|
|
|
![]() |
|
@BigTech, yes, holding down the Alt key is a trick, and it is one that works. :-) Thank you for posting it and a code example.
------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
Do you have an extension that includes holding down Alt for switching windows, to plug into Vocola, please? I'm having trouble getting Mark's WindowGo to work for switching windows that are not minimized to the Taskbar. Apparently he does not use the ALTernative method... I used Alt at one time, but not with Python. That method was disabled by an update to one of the utilities. Thanks. |
|
|
|
![]() |
|
Kevin, indeed, pressing a hotkey is the route to getting the foreground love, and this is what I have been trying to point out to you along the messages in this thread. To no avai, unfortunately.
-------------------------
|
|
|
|
![]() |
|
Foreground love?
SP Application Activation© is not looking for love. Instead it places anything you see (or don't see) in the taskbar to the foreground and activates it. Doesn't matter if it's programs, child windows of programs, specific instances of programs, pictures, Explorer Windows, dialogs, MSG boxes, you name it.
Best of all, it will also allow you to use only part of the target name. How about that for ergonomics
Demonstration Video:
------------------------- Turbocharge your Dragon® productivity with 40 Power Addons |
|
|
|
![]() |
|
Well, Rob, once again you are nowhere near to getting the picture, since the whole thread has been about the Win32 SetForegroundWindow API function not working as expected at all times. -------------------------
|
|
|
|
![]() |
|
@Wilke
The above video obviously proves it can be done reliably, despite the limitations discussed in this thread. I guess Tom, SP PRO programmer knows what he's doing
We invite you to watch it (and be amazed!).
Better yet, try before you buy! After all, there's a free SP 6 PRO Demo available ------------------------- Turbocharge your Dragon® productivity with 40 Power Addons |
|
|
|
![]() |
|
just a follow-up to this thread, I finally got around to trying pressing the alt button and it does seem to improve reliiability. fwiw, here's some sample code in Vocola which uses the Window.Go_ from the window extension that Mark wrote. you will notice I doubled up on the alt_release call, since I found that occasionally alt would be 'down' when sublimetext popped up. but so far so good...
# Sue switch to sublimetext Vocola commands Keys.SendInput({alt_hold}) # apparently this improves the reliability of activating Windows Window.Go_( RegularAnd("Sublime Vocola") "((?!extension).)*$" ) Keys.SendInput({alt_release}) Wait(800) Keys.SendInput({alt_release}) |
|
|
FuseTalk Standard Edition v4.0 - © 1999-2022 FuseTalk™ Inc. All rights reserved.