When started via AutoHotKey, WT is unfocused.
Environment
Windows build number: Microsoft Windows [Version 10.0.21301.1000]
Windows Terminal version (if applicable): 1.6.10272.0
Any other software?
AutoHotKey 1.1.33.02
Steps to reproduce
Use the following AutoHotKey script:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Enter::
Run, wt
; Run, explorer.exe shell:AppsFolder\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe!App
return
Expected behavior
The Windows Terminal window opens up and is focused, so typed input goes into that window. This is how all other applications work (tested with cmd, MS Edge Dev, Mozilla Firefox, and Notepad).
Actual behavior
The Windows Terminal window opens, but is not focused. Instead, the window that was previously focused retains focus. Therefore, input typed goes into the previously focused window instead of the terminal.
Hmmm! I almost always start Windows Terminal via AHK (1.1.30.03) ... using
; Ctrl-Alt-t
!^t::
Run wt.exe
return
It always has focus.
I use
Microsoft Windows 10 Pro for Workstations 10.0.19042.746 (2009, 20H2) WindowsTerminalPreview_1.6.10272.0_x64 AHK 1.1.30.03
I also tried #Enter:: and #>Enter:: (I don't have a left Win key). Both resulted in WT having focus.
But I must have had trouble with focus because several of my key/action definitions contain the line
WinActivate Program Manager ; desktop
Adding that to the #Enter script (before starting WT) may solve the problem.
Wow, thank you @vefatica! Adding that line right before the Run, wt did the trick!
It's not perfect --- if I open two terminal windows in succession and close the second from the keyboard, the focus doesn't go to the first (as you'd expect). But this is an edge case for sure! Thanks for giving me help with the common case.
I'm going to close this for now, since @vefatica came through with a workaround (thanks again!); if someone else has this issue they can re-open or open a new one.
I'm glad it helped.
Funny ... mine worked OK without that line. And when I put that line in mine, I did not have the edge case you described.
Unfortunately it seems like that line only helps some of the time. I tested it a bunch, and it was working 100% at first, but it's inconsistent now.
This is kinda frustrating.
Focus-stealing has never been a problem here. So I have customized a setting to make taking focus easy (because DevEnv has a hard time bringing Firefox to the foreground to show help). Perhaps it will help you. The setting is
HKEY_CURRENT_USER\Control Panel\Desktop ... REG_DWORD ... ForegroundLockTimeout
I set it to 0.
The default value is 200000 (0x30D40).
I'm not sure if there's anything actionable for us to do here. We're not doing anything bizarre like if (launched from AHK) { defocusTheWindow(); }. IMO this sounds like something that AHK would need to figure out on their side.
It kinda reminds me of #4233 (I know there's another, better issue tracking that problem but I can't find it right now).
I was looking around through AHK's documentation last week and found that there was a command to force setting a window to the foreground. Maybe you could use that to manually bring the terminal window to the foreground? Disclaimer: I don't really have any experience with AHK.
It sounds like what you're looking for is something like #653? That might be coming... sooner than previously expected...
I think @zadjii-msft might be referring to AHK's WinActivate command. You might try it after a slight delay, perhaps ...
Run wt.exe
Sleep, 300
WinActivate, ahk_class CASCADIA_HOSTING_WINDOW_CLASS
Setting the variable #WinActivateForce might enhance the WinActivate command.
That's the one, thanks!
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.
Thank you so much @vefatica and @zadjii-msft!
I clean-reinstalled windows, which didn't fix the problem. When I tried the sleep+WinActivate method, the wrong WT window was getting focused at times. Setting the registry key alone didn't work consistently.
I think the combination of the two methods did the trick, though (at least the problem is very rare, if it's still there).
I'm still really curious about the root cause of the problem.
By the way, are there any potential negative effects of setting that registry key?
By the way, are there any potential negative effects of setting that registry key?
I never quite understood what the big deal about focus-stealing was all about. That key is part of a mechanism by which Windows makes it difficult to "steal" focus. So I suppose setting that registry value could have a negative effect, namely, something steals focus when you didn't want it to do so. I must say, that never happens here.
Frankly, I have no idea what the reg key does. With these obscure user32 things, I trust there's a good reason they exist, even if I don't know what that is.
It's entirely possible that the root cause of this is a weird interaction between AHK and Xaml Islands. No way to be sure. There aren't all that many other applications using XI, so this might "only" affect the Terminal, when really it affects all XI apps. That's all conjecture though.
That being said, since you've found a workaround, I'm not sure there's anything else actionable from us here, so I'm gonna close this. Thanks!
I hate to dead-thread bump, but I think I should include this as additional context. I've been using AHK to open up the regular wsl terminal to Ubuntu for quite a while using the following AHK script (opens with Win+o):
#o::
Run,ubuntu2004.exe,,,PID
WinWait ahk_pid %PID%
WinActivate, ahk_pid %PID%
This:
- Always opens up a new Ubuntu WSL Terminal (including when there's an existing one)
- Changes focus to it immediately regardless of existing focus being anywhere else (including another WSL window).
The same code however, doesn't show the same behavior with Windows Terminal. With WinTerm (i.e. replacing line 2 with Run,wt.exe,,,PID ),if there's an existing window, it will not open up a new one. If I remove the WinWait, it'll open up a new one, but not switch focus to the newly-created window. I'm wondering if this has to do with the fact that WinTerm is kinda-sorta-a-UWP-app,-but-not-really-because-it's-supposed-to-launch-like-a-traditional-application...? If I'm seeing things correctly with Window Spy in AHK, it looks like the app starts really fast with a different ahk_pid and then spawns another, which is the one I actually want for WinActivate. I'm guessing it has to do with tabs/panes/etc versus a sort of "main" WinTerm process but I don't know how I'd snatch the child pid properly or if there's a better way.. Not sure if this is ultimately an AHK issue or a Windows Terminal issue. Anyway, the above code might help someone if they're still on the regular WSL terminal and want the above 2 behaviors every time.
I skimmed through, not sure if there was a proper answer but I was having trouble myself with opening terminal using win + t with autohotkeys. Completely new to it but have always used a lot of non-custom system shortcuts. based on some of the answers saying other shortcuts worked for them made me look to see if windows has a default win + t shortcut and they do. iirc it places focus on the taskbar.
tldr: disable the win + t shortcut in regedit, the interfering default windows shortcut should be disabled after a system reboot.
@doubleOZ If you're using AHK just to focus the Terminal window, then you may be interested in the globalSummon action
@zadjii-msft nah, I've got a few hotkey commands I use daily. just thought others should know that if they are having trouble with mapping something to win+t, its because windows already has a shortcut bound to that.
I was observing the same behavior of AHK getting blocked on WinWait described by @schmitmd above, it seems as if AHK doesn't capture the correct PID when wt.exe starts running. I managed to hack around it with the following:
#w::
Run, wt.exe --window 9 --profile "Debian-ranger",,, process_id
Sleep, 150
; Does not work, it seems AHK does not capture the correct PID
; WinWait, ahk_pid %process_id%
; WinActivate, ahk_pid %process_id%
Run, wt.exe --window 9 focus-tab,,,
return