Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Best way to bring window back to focus?

Jul
274
6
I've done this before, but I'm using TCC under Windows Terminal now and I think the window internal command doesn't work under Windows Terminal.

I really wish the "Activate" command could be used on the current window without having to name it, because that would make life easier, and I have tons of windows named "TCC" so it's an ambiguous situation.

Ironically if I type 'activate' with no parameters, the pop-up it brings up refocuses me back the way I would like. If only I could send an ESC with Fakey to it.
 
What is Fakey?

I'm pretty sure TCC has no clue that it's running in WT.

If you have only one instance of WT, any TCC can get the title of WT's active tab. For example.

Code:
v:\> echo %@winapi[user32.dll,FindWindowW,CASCADIA_HOSTING_WINDOW_CLASS,0]
11339484

v:\> echo "%@winapi[user32.dll,GetWindowTextW,11339484,buffer,1024]"
" 30.00.22  [9980]  v:\"
 
What is Fakey?

ignore that. old utility i used to use to send keypresses. there's other ways to do the same.

I'm pretty sure TCC has no clue that it's running in WT.

no, but it's easy to detect

Usually (but not always) it's a matter of:
Code:
  if "%SESSIONNAME%" eq "Console"  (set container=TCC)
  if defined WT_SESSION   (set container=WindowsTerminal)

However this isn't 100% so I also check for an environment variable i set in my TCSTART.btm



If you have only one instance of WT, any TCC can get the title of WT's active tab. For example.

I have 4 monitors. I'm typically going to have 4 instances so i can use the command line on any of my 4 monitors, but that's good to know

Code:
v:\> echo %@winapi[user32.dll,FindWindowW,CASCADIA_HOSTING_WINDOW_CLASS,0]
11339484

v:\> echo "%@winapi[user32.dll,GetWindowTextW,11339484,buffer,1024]"
" 30.00.22  [9980]  v:\"


That's a neat trick! I had to save those to come back to them.




Alas, I just want "window activate" to work the same under WT as under TCC without WT. I want to bring the current instances to forward focus.

An example of when I want to do this is after watching a tv show. I want the command line i launched the tv show from to come back to the front, as the next thing my watch.bat script does is ask me if i want to delete the file of the show i've watched.
 
I'm really impressed with all your knowledge!
 
but yes, i thought "Window activate" would bring the current TCC window to the front.

But that doesn't work inside windows terminal wrapper.

I am in situations where i lose a window but at a certain time i would like it to come back to the front automatically, like when a long job is finished.
 
ACTIVATE won't work in WT because it tries to use SetForegroundWindow on the ConsoleWindowClass window which doesn't exist whan TCC is in WT. A combination of these can do it ...

Code:
v:\> echo %@winapi[user32.dll,FindWindowW,CASCADIA_HOSTING_WINDOW_CLASS,0]
2688772

v:\> delay 10 & echo %@winapi[user32.dll,SetForegroundWindow,2688772]
1

... but Windows may or may not it happen. Windows is fussy about letting apps set the foreground window (google "SetForegroundWindow"; read MS's docs to see the restrictions).

I have a "4WT" plugin which does a few things you'd like. I'll add a FOCUSWT command (which does the above) and get back to you. It will be subject to Windows's restrictions.
 
ACTIVATE won't work in WT because it tries to use SetForegroundWindow on the ConsoleWindowClass window which doesn't exist whan TCC is in WT. A combination of these can do it ...

Code:
v:\> echo %@winapi[user32.dll,FindWindowW,CASCADIA_HOSTING_WINDOW_CLASS,0]
2688772

v:\> delay 10 & echo %@winapi[user32.dll,SetForegroundWindow,2688772]
1

... but Windows may or may not it happen. Windows is fussy about letting apps set the foreground window (google "SetForegroundWindow"; read MS's docs to see the restrictions).

Yea, windows 10 doesn't seem to be letting it happen here. I tried it out.

But also, I typically have 4 WT windows open (one for each monitor), so i'm not sure detecting by the window class will find the right window in this situation?

I have a "4WT" plugin which does a few things you'd like. I'll add a FOCUSWT command (which does the above) and get back to you. It will be subject to Windows's restrictions.

Oooh, that would be amazing. It's not like other programs don't grab focus when interrupt me with their pop-ups. So it's definitely *allowed*. But windows is indeed fussy.
 
And oh, as a quick correction, it was "Window restore" that i used to use to bring my focus back, not activate. My bad. But yea, everything under the "window" command doesn't work under WT.
 
And oh, as a quick correction, it was "Window restore" that i used to use to bring my focus back, not activate. My bad. But yea, everything under the "window" command doesn't work under WT.
And it was my mistake saying ACTIVATE won't work in WT.

Right! WINDOW manipulates the window found with GetConsoleWindow. When TCC is in WT, that function returns a handle to a hidden PseudoConsole window which is not the window you want to manipulate.
 
What an interesting set of circumstances. I would settle for an EXE file that would somehow just "do" it.

I already have a super-handy focus-monitor.exe that just states the process/exe name every time focus changes. It's solved a few mysteries over the years.
 
Back
Top
[FOX] Ultimate Translator
Translate