Bug 484992

Summary: Xwayland apps get Tab keyboard events even when they should be filtered out by event filter
Product: [Plasma] kwin Reporter: mailport+kdebugs
Component: inputAssignee: KWin default assignee <kwin-bugs-null>
Status: REOPENED ---    
Severity: normal CC: alex, aurelien, bluelkshell, brunoades, bugs.kde.org, cameron.w, claudio, contact, descartavel1, dzert127, elman, figueroa628, greg.martyn, h_dieter, jadaml, jbeckman1974, julian, kde, kde, kdebugs, kdedev, mailport+kdebugs, mg05182-kde, moore.bryan, moslike6, nate, nyanpasu64, postix, ricardo, sami.kyostila, schierkevin, sebastian.pb31, sebbarg, srikarbharadwaj0, superrudra1601, tandiv15.bansal, tchiot.ludo, vaiogames18, web, xaver.hugl, xnaxdy, Xtian7489
Priority: HI Keywords: regression
Version: 6.0.3   
Target Milestone: ---   
Platform: Neon   
OS: Linux   
See Also: https://bugs.kde.org/show_bug.cgi?id=478556
Latest Commit: Version Fixed In: 6.2.0
Sentry Crash Report:
Attachments: Video of a window receiving TAB characters while cycling windows.
Video of a key being sent to a XWayland app after it's released in Wayland.
Video of the bug occurring on Fedora Plasma 6.3.0.

Description mailport+kdebugs 2024-04-03 16:23:06 UTC
# SUMMARY

When the current focus in in an XWayland application/windows and I switch to the over-next window (by pressing Alt+Tab+Tab) the second tab will also be registered inside the application

# STEPS TO REPRODUCE

1. Open a xwayland application (preferably one with a textarea input - for easier debugging).
2. Set the input focus into the textarea.
3. Press Alt+Tab+Tab to switch two windows further (aka HOLD-ALT PRESS-TAG PRESS-TAB RELEASE-ALT)
4. Go back to the original window and observe that one tab was pressed 

# OBSERVED RESULT

The second tab of the Alt-Tab-Tab was forwarded (?) to the original window

# EXPECTED RESULT

The task-switcher should "swallow" the key presses (the first tab seems to be swallowed, but not all further presses)

# SOFTWARE/OS VERSIONS

KDE Plasma Version:  6.0.3
KDE Frameworks Version:  6.0.0
Qt Version:  6.6.3
Kernel Version: 6.8.2-arch2-1 (64-bit)
Graphics Platform: Wayland

# ADDITIONAL OBSERVATIONS

 - This only works in XWayland windows (confirmed in IntelliJ Goland, Webstorm, Bitwarden, MongoDB Compass, non-wayland-VSCodium)

 - In native Wayland applications I can't reproduce it (aka firefox, Dolphin, Konsole, ....)

 - The bug is not specific to the tab key, If I [HOLD-ALT] [Tab] [ARROW-DOWN] [ARROW-DOWN] [RELEASE-ALT], then the two arrow presses do also appear in the original application

 - The task-switcher behaves completely normal during all this - meaning both applications (teh task-switcher and the originally focused window) receive the key press

 - Does not happen when I switch to X11

 - Does happen independent which task switcher I chose in the settings (tested "Modern Informative", Compact, "Thumbnail Grid")

 - It happens consistently, I can reproduce it every time I try
Comment 1 Zamundaaa 2024-04-22 14:13:09 UTC
Can confirm, though it only happens if you set X11 apps to always be able to read all keystrokes
Comment 2 mailport+kdebugs 2024-04-22 14:22:01 UTC
(In reply to Zamundaaa from comment #1)
> Can confirm, though it only happens if you set X11 apps to always be able to
> read all keystrokes

Wow - thanks Zamundaaa. That actually fixes my problem (even though I had the option "Any key typed while Control/Alt/Meta are pressed" selected). But switching to "Never" completely fixes it.

You can't believe how annoying the last week was, I think in every project of mine are now random tabs committed.
Really thank you - lol, send me you postal address and I'll mail you a box of chocolate.
Comment 3 Kevin Schier 2024-04-25 12:53:30 UTC
Also happens for me when using the overview feature or desktop grid. First input gets properly intercepted while all following keys "leak" through.
Comment 4 Zamundaaa 2024-04-28 23:10:29 UTC
*** Bug 486263 has been marked as a duplicate of this bug. ***
Comment 5 Nate Graham 2024-06-29 17:54:23 UTC
Trivial way to reproduce: Focus an XWayland window and Alt+Tab with default XWayland snooping settings. The app gets sent tab keys.
Comment 6 Nate Graham 2024-06-29 17:54:30 UTC
*** Bug 489325 has been marked as a duplicate of this bug. ***
Comment 7 Nate Graham 2024-06-29 20:15:48 UTC
*** Bug 489382 has been marked as a duplicate of this bug. ***
Comment 8 Nate Graham 2024-06-29 20:16:15 UTC
*** Bug 488870 has been marked as a duplicate of this bug. ***
Comment 9 Nate Graham 2024-06-29 20:16:46 UTC
This was exposed by changing the XWayland snooping move away from "Never".
Comment 10 Divyanshu 2024-07-02 14:16:10 UTC
*** Bug 489609 has been marked as a duplicate of this bug. ***
Comment 11 Zamundaaa 2024-07-02 19:18:18 UTC
*** Bug 489631 has been marked as a duplicate of this bug. ***
Comment 12 figueroa628 2024-07-02 21:31:05 UTC
Created attachment 171308 [details]
Video of a window receiving TAB characters while cycling windows.
Comment 13 s3bbarg 2024-07-03 11:20:09 UTC
I see it as well. Here is a simple way to reproduce using vscode (it affects chrome as well, but is very easy to see in vscode)

1. Settings -> Task Switcher: disable "show selected window" (makes it easier to see what is happening)
2. start vscode with an empty document
3. press alt-tab repeatedly and observe the tab character "bleeds through" to vscode.
Comment 14 Zamundaaa 2024-07-04 22:35:15 UTC
*** Bug 489757 has been marked as a duplicate of this bug. ***
Comment 15 JC 2024-07-08 17:06:44 UTC
Add backtick to the list: when pressing Alt+` + ` (backtick twice) the original window gets the backtick.
Comment 16 Bug Janitor Service 2024-07-09 13:42:19 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/6074
Comment 17 David Edmundson 2024-07-24 12:50:52 UTC
*** Bug 490754 has been marked as a duplicate of this bug. ***
Comment 18 elman 2024-07-26 02:19:13 UTC
I actually started to report new bug when I stumbled upon this one, so here is my description which seems pretty much like what is described here:

When I use Alt+Tab to switch windows, keystrokes are sent to last active window and appear as spaces. For example, I use RubyMine, have code `<MessageBox messages={messages}/>` and I leave cursor between `=` and `{`. Then I use Alt+Tab to loop through all open windows (13 window) and get back to RubyMine, I have code `<MessageBox messages=                         {messages}/>` so 25 spaces are added.

So far I have seen this issue with RubyMine, Typora.

Slack and Spotify act as if they are receiving Tab keystrokes.

All of these applications are xclients.
Comment 19 David Edmundson 2024-08-02 09:40:42 UTC
Git commit 8fd4476ff1848ce7ce4d3573224fc4893ae8339d by David Edmundson.
Committed on 02/08/2024 at 09:31.
Pushed by davidedmundson into branch 'master'.

wayland: Move XWayland key forwarding into a filter

We optionally send some keys to xwayland through the filter when no x11
client has focus. This allows shortcut handling in X11 apps to work.

When kwin is grabbing keys we don't necessarily want X11 to sniff these
keys as things can get out of sync. A key place is the tabbox. The X11
client sill has focus, but xwayland is not active. This means we pass
tab keys to X which then go to application incorrectly.

Part of this patch changes the tabbox filter to not intercept the alt
key release event. This ensures xwayland's concept of pressed modifiers
stays in sync.

M  +36   -0    autotests/integration/x11keyread.cpp
M  +1    -0    src/input.cpp
M  +1    -0    src/input.h
M  +50   -49   src/xwayland/xwayland.cpp
M  +2    -2    src/xwayland/xwayland.h

https://invent.kde.org/plasma/kwin/-/commit/8fd4476ff1848ce7ce4d3573224fc4893ae8339d
Comment 20 fanzhuyifan 2024-08-05 04:51:40 UTC
*** Bug 491287 has been marked as a duplicate of this bug. ***
Comment 21 ⭐️NINIKA⭐️ 2024-08-08 09:21:52 UTC
 I see that it "Version fixed in" says 6.2.0, the next major release, which is still some time off.

Would it be possible to cherry-pick it into some of the 6.1 bugfix releases?
Comment 22 elman 2024-08-08 22:21:23 UTC
(In reply to ⭐️NINIKA⭐️ from comment #21)
>  I see that it "Version fixed in" says 6.2.0, the next major release, which
> is still some time off.
> 
> Would it be possible to cherry-pick it into some of the 6.1 bugfix releases?

At Nate's blog I noticed that this only happens with default setting "When Alt+Tabbing through windows, tab keystrokes no longer leak into XWayland-using apps when using default XWayland app keyboard snooping setting". So I just went to settings and selected to never send any keystrokes to X11 apps and that works for me. I don't use any X11 app that would need those keystrokes.
Comment 23 duha.bugs 2024-08-27 14:47:03 UTC
*** Bug 492272 has been marked as a duplicate of this bug. ***
Comment 24 Nate Graham 2024-08-27 20:01:52 UTC
*** Bug 492116 has been marked as a duplicate of this bug. ***
Comment 25 Nate Graham 2024-08-27 20:01:55 UTC
*** Bug 492037 has been marked as a duplicate of this bug. ***
Comment 26 Ricardo J. Barberis 2024-08-28 01:30:23 UTC
(In reply to elman from comment #22)
> (In reply to ⭐️NINIKA⭐️ from comment #21)
> >  I see that it "Version fixed in" says 6.2.0, the next major release, which
> > is still some time off.
> > 
> > Would it be possible to cherry-pick it into some of the 6.1 bugfix releases?

Please KDE devs, if it's not too invasive it'd be nice to have this backported to 6.1.x
 
> At Nate's blog I noticed that this only happens with default setting "When
> Alt+Tabbing through windows, tab keystrokes no longer leak into
> XWayland-using apps when using default XWayland app keyboard snooping
> setting". So I just went to settings and selected to never send any
> keystrokes to X11 apps and that works for me. I don't use any X11 app that
> would need those keystrokes.

Thanks for pointing out that workaround!
It never occurred to me to go looking through the settings.

I don't know what side effects this setting might have but at least I won't lose my mind now 😁
Comment 27 Zamundaaa 2024-09-01 22:46:55 UTC
*** Bug 492491 has been marked as a duplicate of this bug. ***
Comment 28 Zamundaaa 2024-10-02 14:27:10 UTC
*** Bug 493956 has been marked as a duplicate of this bug. ***
Comment 29 Nate Graham 2024-10-04 23:36:22 UTC
*** Bug 492361 has been marked as a duplicate of this bug. ***
Comment 30 Nate Graham 2024-10-04 23:36:25 UTC
*** Bug 493221 has been marked as a duplicate of this bug. ***
Comment 31 Nate Graham 2024-10-04 23:36:28 UTC
*** Bug 493386 has been marked as a duplicate of this bug. ***
Comment 32 Bryan Moore 2024-11-14 15:37:10 UTC
This does not appear fixed in 6.2.3 on Fedora 41.
Comment 33 Nate Graham 2024-12-12 19:24:28 UTC
*** Bug 494590 has been marked as a duplicate of this bug. ***
Comment 34 nyanpasu64 2025-02-05 08:55:26 UTC
Created attachment 177983 [details]
Video of a key being sent to a XWayland app after it's released in Wayland.

I'm still getting this bug on KDE Plasma 6.2.5 on Fedora 41. To understand why the bug happens, I installed screenkey, a X11-based program which displays keystrokes on-screen. Since it's a X11 app running under XWayland, it only sees keystrokes delivered to XWayland apps. 

## Reproduction

To set up keystroke visualization, in Screenkey settings I checked "Persistent window", and under KDE "Special Application Settings" I set all windows from Screenkey to be above other windows. Then I restarted screenkey, dragged the window to a convenient location, then opened a Wayland app (Firefox) and an X11 app (Obsidian Flatpak).

To reproduce the bug, I typed into Wayland Firefox while watching screenkey to see what keystrokes it's receiving. I also moved my mouse in circles over X11 Obsidian, which makes XWayland send events to screenkey more quickly. I used GPU Screen Recorder in replay mode to record the screen while I looked for bugs, and in recording mode to capture a screencast of the bug happening. My findings:

## Behavior

- Enter, Backspace, Tab, and arrow/Home/End/Page Up/Down keystrokes are delivered to screenkey immediately. If you're not moving the mouse, they get delayed until the next modifier keystroke (like Ctrl or Shift) or a periodic timer.
- Modifier shortcuts (like Ctrl+A) are delivered to screenkey immediately. If you're not moving the mouse, they're delayed until you release Ctrl.
- If I release Ctrl before A, XWayland usually enters a state where it sends an endless string of A keystrokes to screenkey (which refreshes faster if you're moving your mouse over a X11 app); I think the key repeats forever because kwin_wayland fails to deliver the A key release to XWayland. While the key is repeating, if I mouse-click into a X11 (XWayland) app, there is a 10-50% chance that an erroneous A key gets delivered to the app. I think this happens if a key repeat gets delivered to the app before XWayland realizes A is no longer being pressed.

I've reproduced this bug with alphabetic and symbol keys (like ; semicolon). I'm not sure this bug happens with *tab* events after Plasma 6.2; Tab is always sent to XWayland (so letting go of a modifier does *not* cause the key release to not be sent), Alt+Tab does not appear on Screenkey at all, and Ctrl+Tab does not trigger endless repeats on screenkey if I release Ctrl before Tab (I think the endless repeats are what causes stray keystrokes).

In any case I'm still posting this report under this bug; I reported the issue with alphabetic keys instead of Tab at Bug 493956, but that got closed as a duplicate of this.

## Fix

I'm guessing the fix is that if the user presses Ctrl+A, the A keypress "belongs to a shortcut" and the key release is unconditionally delivered to XWayland, even if Ctrl has been released already. Also, if the user presses A and it's not delivered to XWayland, then presses Ctrl and releases A, it may not be necessary to deliver the key release if you don't deliver key-held events; I don't know if you do or not, or what behavior is ideal.

I think this behavior is a result of KDE's implementation of "Allow legacy X11 apps to read keystrokes typed in all apps:" "As above, plus any key typed while the Control, Alt, or Meta keys are pressed" option. Unfortunately this is the default value of the setting.

Hopefully this analysis helps find out *why* stray keys are sent to XWayland apps, and fix keys being stuck on repeat on XWayland and being sent to apps when focused. I wonder if there's a similar bug where if you hold Ctrl+A on a Wayland app and click on a X11 app, KWin sends a stray Ctrl+A to the app. I don't think this bug exists, since when you click on a X11 app, screenkey reports a single A keystroke (not a Ctrl+A) and spends a second before repeating Ctrl+A presses. I also tried this procedure 20+ times and did not see the bug.

Operating System: Fedora Linux 41
KDE Plasma Version: 6.2.5
KDE Frameworks Version: 6.10.0
Qt Version: 6.8.1
Kernel Version: 6.12.9-200.fc41.x86_64 (64-bit)
Graphics Platform: Wayland
Processors: 4 × Intel® Core™ i5-3570K CPU @ 3.40GHz
Memory: 7.6 GiB of RAM
Graphics Processor: Mesa Intel® HD Graphics 4000
Manufacturer: M&A Technology
Product Name: DB75EN__
Comment 35 TraceyC 2025-02-05 16:41:34 UTC
Reopening, given the new information.
Thanks for the detailed troubleshooting!
Comment 36 TraceyC 2025-02-05 16:41:44 UTC
.
Comment 37 David Edmundson 2025-02-06 15:09:52 UTC
I've been trying for a while to reproduce with the steps in #34 and I'm failing to hit an issue with unexpected 'a' keys being sent to an application after releasing keys.

There have been changes in kwin in this area between your testing and current master, it would be useful if you can confirm you still have an issue with 6.3 is out.
Comment 38 TraceyC 2025-02-06 20:45:56 UTC
I tested this on git-master  and Plasma 6.2.5

Unfortunately, I can't test this with screenkey. Note that Screenkey is not supported under Wayland
On Solus, it doesn't have a system tray icon, so I can't get to its settings. It was introducing lag, and preventing keystrokes from getting to applications. It made the system fairly unusable. Without screenkeys, those same keystrokes were input into Wayland windows as expected, so IMO it's not a reliable testing tool in Wayland.

TEST STEPS - From original post - Alt + tab + tab
1. Open a xwayland application - used Obsidian Notes
2. Set the input focus into the textarea.
3. Press Alt+Tab+Tab to switch two windows further (aka HOLD-ALT PRESS-TAG PRESS-TAB RELEASE-ALT)
4. Go back to the original window and observe that one tab was pressed 

Results: 
git-master: Unable to reproduce 
6.2.5: I do see undesirable results
When Obsidian is focused and I press Alt Tab Tab, the window goes white and a Tab character is inserted in the note
After doing that a couple of times, it stopped happening.

TEST STEPS - From comment #34
Used Obsidian and Kate to test

1. Set the cursor in Obsidian to be inside a note
2. Type text into Kate. Use various shortcuts (Ctrl-A Ctrl-C Ctrl-Shift-S)
3. Alt + tab + tab to other apps and Alt+tab back
4. Entered Enter, Backspace, Tab, and arrow/Home/End/Page Up/Down keystrokes 

Results: 
git-master: Unable to reproduce the bug, no stray keys appears in Obsidian
6.2.5: Unable to reproduce
Comment 39 TraceyC 2025-02-10 16:58:19 UTC
I have noticed stray characters in Obsidian in Plasma 6.2.5, intermittently

Steps:
1. Have Obsidian Notes or another xwayland app open, that can accept text
2. Go to a Wayland window and use Ctrl+C to copy some text
3. Switch back to the Obsidian window by clicking it

Observed: A 'c' character is pasted into Obsidian without me doing anything
It has been intermittent, sometimes happening a few times in a row, and then not happening for a while
Comment 40 TraceyC 2025-02-10 21:26:48 UTC
I've been testing the stray character thing on and off all day and can't reproduce on git-master at all.
This potentially fixed in 6.3.0
Comment 41 Vringar 2025-02-12 23:34:09 UTC
(In reply to TraceyC from comment #40)
> I've been testing the stray character thing on and off all day and can't
> reproduce on git-master at all.
> This potentially fixed in 6.3.0

I can confirm that the upgrade to 6.3.0 fixed this issue for me.
Comment 42 nyanpasu64 2025-02-13 02:56:42 UTC
Created attachment 178241 [details]
Video of the bug occurring on Fedora Plasma 6.3.0.

The stray character bug still happens on Plasma 6.3.

When I set KDE Wayland's X11 key visibility to the default of "As above, plus any key typed while the Control, Alt, or Meta keys are pressed", Screenkey shows the "X11 never sees the key is released" bug is still happening unchanged, and the key repeats can still register as characters in Obsidian.

Operating System: Fedora Linux 41
KDE Plasma Version: 6.3.0
KDE Frameworks Version: 6.10.0
Qt Version: 6.8.2
Kernel Version: 6.12.11-200.fc41.x86_64 (64-bit)
Graphics Platform: Wayland
Processors: 4 × Intel® Core™ i5-3570K CPU @ 3.40GHz
Memory: 7.6 GiB of RAM
Graphics Processor: Intel® HD Graphics 4000
Manufacturer: M&A Technology
Product Name: DB75EN__
Comment 43 TraceyC 2025-02-13 16:55:02 UTC
Thanks for the updated information and video. That clearly shows the problem happening. Good catch on the related setting.


I re-tested in 6.3.0 and git-master

I checked KDE Wayland's X11 key visibility, verified "As above, plus any key typed while the Control, Alt, or Meta keys are pressed"
(Settings->Application Permissions->Legacy X11 App Support)
I used Firefox and Obsidian Notes and repeated what I did in comment #38

6.3.0: I can still reproduce, intermittently. Sometimes Ctrl+C did not always result in a 'c' in Obsidian. Ctrl+f in Firefox did result in an 'f' character in Obsidian.

git-master: X11 keystrokes had been set to "Always". Changed to the default. Still unable to reproduce at all.
Comment 44 Bug Janitor Service 2025-02-28 03:46:44 UTC
🐛🧹 ⚠️ This bug has been in NEEDSINFO status with no change for at least 15 days. Please provide the requested information, then set the bug status to REPORTED. If there is no change for at least 30 days, it will be automatically closed as RESOLVED WORKSFORME.

For more information about our bug triaging procedures, please read https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging.

Thank you for helping us make KDE software even better for everyone!
Comment 45 nyanpasu64 2025-02-28 04:20:36 UTC
See previous comments.
Comment 46 Ricardo J. Barberis 2025-03-01 18:25:35 UTC
I still see this behaviour with the default settings at least when Alt+Tabbing from a Wayland window to an XWayland window, e.g. when switching from Okular to Opera, the browser's menu opens due to the Alt key leaking.
Comment 47 Ricardo J. Barberis 2025-03-01 18:28:55 UTC
Updated system info:

Operating System: Fedora Linux 41
KDE Plasma Version: 6.3.2
KDE Frameworks Version: 6.11.0
Qt Version: 6.8.2
Kernel Version: 6.13.5-200.fc41.x86_64 (64-bit)
Graphics Platform: offscreen
Processors: 12 × AMD Ryzen 5 5600H with Radeon Graphics
Memory: 15.0 GiB of RAM
Comment 48 Ricardo J. Barberis 2025-03-01 18:31:02 UTC
Sorry, kinfo but this time logged in graphically and not via ssh:

Operating System: Fedora Linux 41
KDE Plasma Version: 6.3.2
KDE Frameworks Version: 6.11.0
Qt Version: 6.8.2
Kernel Version: 6.13.5-200.fc41.x86_64 (64-bit)
Graphics Platform: Wayland
Processors: 12 × AMD Ryzen 5 5600H with Radeon Graphics
Memory: 15.0 GiB of RAM
Graphics Processor: AMD Radeon Graphics