Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot type in new pane or tab until focus is manually activated with mouse click #13388

Open
praimmugen opened this issue Jun 27, 2022 · 183 comments
Labels
Area-Windowing Window frame, quake mode, tearout Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.

Comments

@praimmugen
Copy link

praimmugen commented Jun 27, 2022

Windows Terminal version

1.14.1451.0

Windows build number

10.0.19042.0

Other Software

No response

Steps to reproduce

Create new pane/new tab

Expected Behavior

It is possible to type in the newly opened terminal

Actual Behavior

Unable to type until I click on the new terminal.

Note that enabling the option "Automatically focus pane on mouse hover" partially prevents the issue from happening.

It seems like focus is removed from all terminal tabs/panes whenever a new one is created.

@praimmugen praimmugen added the Issue-Bug It either shouldn't be doing this or needs an investigation. label Jun 27, 2022
@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Jun 27, 2022
@praimmugen praimmugen changed the title Cannot type in new pane or tab until focus is manually actived with mouse click Cannot type in new pane or tab until focus is manually activated with mouse click Jun 27, 2022
@lhecker
Copy link
Member

lhecker commented Jun 27, 2022

I can't reproduce this issue, unless it's an initial launch of WSL. Does your issue happen with WSL or with any shell?

@praimmugen
Copy link
Author

Hey, thanks for having a look at this. At the moment I can reproduce it with both WSL and Powershell profiles. However it's a lot less consistent than I thought initially.

It doesn't make much sense, but mostly, after starting a new terminal, new tabs/panes are working fine (apart from the initial launch of WSL as you also noticed). However, if I switch to a different window for some time (seems like around a minute is enough, but 30 seconds is not), then go back to Windows Terminal (using Alt+Tab or mouse doesn't make a difference), I can experience the issue.

At the moment the option "Automatically focus pane on mouse hover" is disabled, so I don't think that was necessarily making any difference like I said in the initial report.

In case it helps, I'm attaching my settings.json

winterm-settings.json.zip

@jamesmcdonald
Copy link

I have an issue that might be related. If I open a terminal from the Start menu in Windows 11 by typing to search for "terminal", it almost always appears under other windows and fails to get focus. If I just click on the pinned icon in the Start menu without typing, this never happens. Opening a terminal with Win-x i also gets focus correctly, as does Ctrl-Shift-n from another terminal. This seems to be consistent with both Terminal (1.13.11432.0) and Terminal Preview (1.15.1863.0).

My default profile is WSL, in case that makes a difference. The issue affects any new windows regardless of how many terminals I have open.

@elouanKeryell-Even
Copy link

same issue here, cannot seem to get the focus on a newly opened tab

@zadjii-msft
Copy link
Member

I'm gonna focus in on the PowerShell repro case here. The fact that the Terminal loses focus when a WSL tab is opened is a well-known bug (I believe in WSLg).

I don't have any idea why this would happen. @praimmugen when the terminal does get into this state (where apparently nothig is focused), what does pressing tab do? That should move focus to the "next" UI element, which might help us figure out where focus is currently.

@zadjii-msft zadjii-msft added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Jul 26, 2022
@praimmugen
Copy link
Author

when the terminal does get into this state (where apparently nothig is focused), what does pressing tab do? That should move focus to the "next" UI element, which might help us figure out where focus is currently.

Unfortunately, pressing tab has no effect - I can't see anything getting focus no matter how many times I press the key.

I'm gonna focus in on the PowerShell repro case here. The fact that the Terminal loses focus when a WSL tab is opened is a well-known bug (I believe in WSLg).

Not sure it changes things, but in my case focus is also lost when opening new WSL panes within a tab.

@ghost ghost added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Jul 26, 2022
@zadjii-msft
Copy link
Member

Is there anything in your PowerShell profile? If pressing tab does nothing, then I'd suspect that maybe the Terminal HWND itself doesn't even have focus, and something else is the forground window. Maybe there's something in your profile.ps1 that's like, shelling out to WSL or something

@zadjii-msft zadjii-msft added Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something and removed Needs-Attention The core contributors need to come back around and look at this ASAP. labels Jul 26, 2022
@praimmugen
Copy link
Author

Nothing in my PS profile, it's completely empty.

Long shot, but I also tried stopping a few apps that were running in background (things like AltDrag, AutoHotkey scripts, X server and several others), but unfortunately it didn't help.

@ghost ghost added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Jul 27, 2022
@zadjii-msft
Copy link
Member

zadjii-msft commented Aug 1, 2022

I WONDER.

I wonder if this is another facet of #13589. Like, if the WS_TOOLWINDOW created by conpty becomes the foreground window for some reason? It absolutely never should, it's not visible but... clearly there's something weird happening. Maybe this is a clue as to why on Windows 10 the taskbar seems to think that it should restore the hidden window first. Maybe that's why we can't repro this either, we're checking on Windows 11. Hmm.

NOTE TO SELF: Assigning to myself to investigate on the Win10 VM to see if I can't repro this.

Also use as an opportunity to double check the taskbar code, and see what heuristic they're using, and why it thinks the conpty window is the one it should operate on.

@zadjii-msft zadjii-msft added Product-Terminal The new Windows Terminal. Priority-2 A description (P2) Area-Windowing Window frame, quake mode, tearout labels Aug 1, 2022
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Aug 1, 2022
@zadjii-msft zadjii-msft added this to the Terminal v1.16 milestone Aug 1, 2022
@zadjii-msft zadjii-msft self-assigned this Aug 1, 2022
@zadjii-msft zadjii-msft removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Attention The core contributors need to come back around and look at this ASAP. labels Aug 1, 2022
@matthuisman
Copy link

matthuisman commented Aug 8, 2022

I have similar issue, but only using command prompt in terminal.
Does not get focus. Need to click to allow typing
Multiple laptops at my company have same issue - so it may be something they have installed...

@MichaelMcCoyTech

This comment was marked as duplicate.

@wiz0u
Copy link

wiz0u commented May 9, 2024

Yeah, Terminal is basically unusable with Visual Studio console programs unless this is fixed. You don't want to need a mouse click when you run/debug your program with F5, before typing things in the console.

@asineth0
Copy link

I'm not sure why #13640 was closed since it is still an issue that happens on the latest version. This isn't the same issue either, but it was closed as a duplicate anyway by someone who's GH account has been deleted.

TL;DR: can we re-open #13640?

@trueuto
Copy link

trueuto commented Jun 27, 2024

🎂 With a 2nd anniversary of this bug I wish everyone a quick breakthrough! It's been a pain in the neck for all users as well as developers as they are trying to fix something caused by the 3rd party apps in fact.

These words are primarily aimed at you, @zadjii-msft - I hope this will be solved soon and you can forget about that once and for all and focus on some other, most likely much more interesting work!

And thanks for all the efforts to have it sorted out!

@george-tsiros
Copy link

Visual studio, start debugging console prog.
Terminal is configured to not use tabbed documents.
If another Terminal window is already open, new window is created unfocused.
If new window is the only Terminal window, it is focused.

@MichaelMcCoyTech
Copy link

Please fix this. It is infuriating and causes me lots of failed logins etc. As you can never rely on terminal retaining focus as you are typing.

@Lionela26
Copy link

Windows Terminal version

1.14.1451.0

Windows build number

10.0.19042.0

Other Software

No response

Steps to reproduce

Create new pane/new tab

Expected Behavior

It is possible to type in the newly opened terminal

Actual Behavior

Unable to type until I click on the new terminal.

Note that enabling the option "Automatically focus pane on mouse hover" partially prevents the issue from happening.

It seems like focus is removed from all terminal tabs/panes whenever a new one is created.

@justineakehurst
Copy link

At it appears this is an interaction between Windows Terminal (reproducible with 1.20.11781.0) and BeyondTrust (reproducible with BeyondTrust Client version 23.9.225.0, PMC adapter 23.9.578) on Windows 11 Enterprise (10.0.22621 Build 22621), is it possible that the Terminal team can assemble an equivalent environment to get a repro, and work with BeyondTrust dev team to determine what might be going on here and find a solution?

@NinoDLC
Copy link

NinoDLC commented Aug 1, 2024

I don't have BeyondTrust, I don't have any shitty bloatware installed, I even reformatted my computer since I had this issue, and it's still there, on a fresh windows 11 install. The only "exotic" stuff I have installed is WSL 2.0

@zoulja

This comment was marked as off-topic.

zadjii-msft added a commit that referenced this issue Aug 29, 2024
Worked with @ekoschik on this one. 

## Bug the first: the MSAL window `ixptools` spawns

> The auth prompt in pwsh.exe is disabling the terminal window while its
opened and re-enabling it when the window closes. BUT it is enabling
Terminal after dismissing itself, instead of before, which means
terminal is disabled when activated.
> 
> Terminal wants focus on the ISLAND window (a grandchild; island is
parented to bridge, which is parented to terminal’s TLW). When it is
activated, it gets a `WM_SETFOCUS` (in response to DefWindowProc
`WM_ACTIVATE`). From `WM_SETFOCUS` it calls `SetFocus` on the bridge
window, and similarly the bridge calls `SetFocus` on the island.
> 
> If the TLW is disabled, these `SetFocus` calls fail (see [this
check](#internal-link-redacted) in `SetFocus`). In the case above, this
leaves Terminal’s TLW as focus, and it doesn’t handle keyboard input.
Note that the window IS foreground/active, but because focus is not on
the island it doesn’t see the keyboard input. Another thing to note is
that clicking on the space to the right of the tabs does NOT revive
keyboard input, but clicking on the tabs or main area does.

> **I recommend having the TLW handle WM_ENABLE and call SetFocus on the
island window.**

And guess what, that works!

## Bug the second: When sublime text is the git `EDITOR`, it doesn't
toss focus back to the Terminal


> In this case, Sublime is calling SFW on the pseudo console window. I
don’t have its code, but it is presumably doing something like
SetForegroundWindow(GetConsoleWindow()). This queues an event to the
pseudo window, and when that event is processed the pseudo window
becomes the active and focus window on the queue (which is shared with
Terminal).
> 
> The sublime window dismisses itself and does the above SFW call.
Dismissing immediately activates the Terminal TLW, which does the
triple-focus dance (TLW sets focus on itself, then bridge, then island).
This completes but is overwritten immediately when the pseudo window
activates itself. Note that the pseudo window is active at this point
(not the terminal window).

> **I recommend having the Pseudo console window handle WM_ACTIVATE by
calling SetFocus on the island window (and not passing the message to
DefWindowProc).**

And guess what, that works!


----

Closes #15956 (I did test this)
This might be related to #13388, we'll have folks try canary and check
@zadjii-msft
Copy link
Member

@NinoDLC FWIW there's a known issue in WSLg that causes the wslg interop to steal focus from the terminal whenever WSL starts up. This thread is specifically for the mysterious sources of focus being lost that don't involve WSL spawning.
Go see:
microsoft/wslg#998
(et al)

@zadjii-msft
Copy link
Member

We merged in a fix that we think is tangential to this problem over in #17828 just today. It should be in this evening's Canary build. Tomorrow, could someone who was seeing the non-WSL focus lost issue test out Canary with a version greater than 1.23.2391.0? That might help us see if this fixed it or not.

You can grab terminal canary from https://aka.ms/terminal-canary-installer

(we'll all be out for the long weekend, so I won't actually know the next canary's build number till like Tuesday)

@zadjii-msft zadjii-msft modified the milestones: Terminal v1.21, Backlog Aug 29, 2024
@zadjii-msft zadjii-msft removed their assignment Aug 29, 2024
@ashpika40
Copy link

I'd be happy to. Will do it tomorrow.

@trueuto
Copy link

trueuto commented Aug 30, 2024

I have just tested the Canary version (1.23.2421.0) and it looks very good! 🎉

There STILL IS some focus loss, but it is very short (blink of an eye, literally) and focus immediately returns back to the WT. If that temporary focus loss is long enough to interfere with input - this I cannot tell. But definitely there is a great step forward! 💪

The temporary focus loss is manifested by the mouse cursor re-appearing on the screen (after it has disappeared on keyboard input) and if you use e.g. a red window background for inactive WT window - you can see it blink. This happens ~30 seconds after WT launch/new tab opened too.

@zadjii-msft
Copy link
Member

I'm super glad to hear it!

There STILL IS some focus loss, but it is very short ...

The temporary focus loss is manifested by the mouse cursor re-appearing on the screen (after it has disappeared on keyboard input) and if you use e.g. a red window background for inactive WT window - you can see it blink. This happens ~30 seconds after WT launch/new tab opened too.

That sounds telltale like some other application is opening for an imperceivable amount of time, then immediately closing. We basically found a slower version of this that involved a git MSAL authentication prompt. That was a clearer example to trace of "another window appears briefly, then closes itself". I doubt whatever is spawning on your machine is an MSAL prompt, but I bet the side effects are similar.

We probably won't be able to do anything about that <1s gap where another window opens into the FG and steals focus, but this at least feels like it'll no longer be blocking for folks.

@trueuto
Copy link

trueuto commented Aug 30, 2024 via email

@trueuto
Copy link

trueuto commented Aug 30, 2024

Tested with script from #13388 (comment)

[08/30/2024 13:53:48] Starting WT...
[08/30/2024 13:53:48] ...done
[08/30/2024 13:53:48] Current WT handle: 1118350
[08/30/2024 13:53:48] Current WT process: 8828 / 29892
[08/30/2024 13:53:48] Wating for focus issue to occur.
[08/30/2024 13:53:48] Got new window handle (3409258) after 00:00:00.5859006 - New process: PID: 12252 / TID: 16208 / Name: WindowsTerminal
[08/30/2024 13:54:21] Got new window handle (462284) after 00:00:32.8106372 - New process: PID: 18348 / TID: 13912 / Name: pwsh
[08/30/2024 13:54:21] Got new window handle (3409258) after 00:00:00.0176991 - New process: PID: 12252 / TID: 16208 / Name: WindowsTerminal

It seems it is the same story, where the focus is lost for the shell process that was started in the WT (pwsh in this case), but that happens for much <1s as can be seen above and focus goes immediately back to the WT itself.

@lhecker
Copy link
Member

lhecker commented Aug 30, 2024

That back-and-forth focus tossing is exactly what I was hoping to address with #17829. We'll probably merge that next week and see if that fixes the remaining issues.

@japurcell
Copy link

In my case, Terminal works just fine until I open the Outlook Office 365 Client. I can navigate panes using keyboard shortcuts like Alt + Up / Down but can't select with mouse. Toolbar buttons are also disabled. When I close Outlook, it works just fine again. I also tried the Canary release mentioned above with the same results. I'm using the Outlook web client as a workaround.

OS Name:                   Microsoft Windows 11 Enterprise
OS Version:                10.0.22631 N/A Build 22631

@jgilles
Copy link

jgilles commented Aug 30, 2024 via email

@ashpika40
Copy link

It works. It still loses focus for a split second and comes back. If you keep typing when this happens you might loose a character, Other than that, excellent job. Finally, I will shift to Terminal completely . Tested on win 10 enterprise 22H2 with beyondTrust and using gitbash for testing

DHowett pushed a commit that referenced this issue Sep 4, 2024
Worked with @ekoschik on this one.

## Bug the first: the MSAL window `ixptools` spawns

> The auth prompt in pwsh.exe is disabling the terminal window while its
opened and re-enabling it when the window closes. BUT it is enabling
Terminal after dismissing itself, instead of before, which means
terminal is disabled when activated.
>
> Terminal wants focus on the ISLAND window (a grandchild; island is
parented to bridge, which is parented to terminal’s TLW). When it is
activated, it gets a `WM_SETFOCUS` (in response to DefWindowProc
`WM_ACTIVATE`). From `WM_SETFOCUS` it calls `SetFocus` on the bridge
window, and similarly the bridge calls `SetFocus` on the island.
>
> If the TLW is disabled, these `SetFocus` calls fail (see [this
check](#internal-link-redacted) in `SetFocus`). In the case above, this
leaves Terminal’s TLW as focus, and it doesn’t handle keyboard input.
Note that the window IS foreground/active, but because focus is not on
the island it doesn’t see the keyboard input. Another thing to note is
that clicking on the space to the right of the tabs does NOT revive
keyboard input, but clicking on the tabs or main area does.

> **I recommend having the TLW handle WM_ENABLE and call SetFocus on the
island window.**

And guess what, that works!

## Bug the second: When sublime text is the git `EDITOR`, it doesn't
toss focus back to the Terminal

> In this case, Sublime is calling SFW on the pseudo console window. I
don’t have its code, but it is presumably doing something like
SetForegroundWindow(GetConsoleWindow()). This queues an event to the
pseudo window, and when that event is processed the pseudo window
becomes the active and focus window on the queue (which is shared with
Terminal).
>
> The sublime window dismisses itself and does the above SFW call.
Dismissing immediately activates the Terminal TLW, which does the
triple-focus dance (TLW sets focus on itself, then bridge, then island).
This completes but is overwritten immediately when the pseudo window
activates itself. Note that the pseudo window is active at this point
(not the terminal window).

> **I recommend having the Pseudo console window handle WM_ACTIVATE by
calling SetFocus on the island window (and not passing the message to
DefWindowProc).**

And guess what, that works!

----

Closes #15956 (I did test this)
This might be related to #13388, we'll have folks try canary and check

(cherry picked from commit 17a55da)
Service-Card-Id: PVTI_lADOAF3p4s4AmhmQzgSwIkE
Service-Version: 1.22
@zadjii-msft zadjii-msft added the Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. label Sep 5, 2024
@zadjii-msft zadjii-msft modified the milestones: Backlog, Terminal v1.23 Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Windowing Window frame, quake mode, tearout Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.
Projects
None yet
Development

No branches or pull requests