Bug 511173 - clipboard causes loss of focus
Summary: clipboard causes loss of focus
Status: RESOLVED UPSTREAM
Alias: None
Product: kwin
Classification: Plasma
Component: general (other bugs)
Version First Reported In: 6.5.0
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords: regression, wayland-only
Depends on:
Blocks:
 
Reported: 2025-10-26 17:29 UTC by gbqofoui
Modified: 2025-10-31 21:23 UTC (History)
5 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description gbqofoui 2025-10-26 17:29:07 UTC
SUMMARY
Plasma 6.5.0 seems to loose keyboard focus when copying to the clipboard. Encountered when using neovim. key-repeat when holding down `x` in neovim normal mode not working and only deleting a single character when using the `vim.opt.clipboard = 'unnamedplus'` setting.

I have tested this with `nvim` in Konsole and Terminator and with `neovide`. All behave the same.

STEPS TO REPRODUCE
1.  write the following to `/tmp/init.lua`
```
local function log_event(event)
  local f = io.open("/tmp/focus.log", "a")
  f:write(string.format("[%s] %s\n", os.date("%Y-%m-%d %H:%M:%S"), event))
  f:close()
end

vim.api.nvim_create_autocmd("FocusLost", { callback = function() log_event("FocusLost") end })
vim.api.nvim_create_autocmd("FocusGained", { callback = function() log_event("FocusGained") end })
log_event("Start")

vim.opt.clipboard = 'unnamedplus'
```
2. run `echo "this is an example" > /tmp/example.txt`
3. in a separate terminal run `touch /tmp/focus.log && tail -f /tmp/focus.log`
4. run `nvim -u /tmp/init.lua /tmp/example.txt`
5. press and hold `x`

OBSERVED RESULT
only one character is deleted and `focus.log` indicates that focus was lost and immediately regained.

EXPECTED RESULT
holding down `x` should initiate key-repeat and the entire line should be deleted. The focus log should remain empty other than the `Start` message. This is the case for Plasma 6.4.5 and Gnome.


SOFTWARE/OS VERSIONS
Operating System: Arch Linux 
KDE Plasma Version: 6.5.0
KDE Frameworks Version: 6.19.0
Qt Version: 6.10.0
Kernel Version: 6.12.53-1-lts (64-bit)
Graphics Platform: Wayland
neovim: v0.11.4


ADDITIONAL INFORMATION
Comment 1 TraceyC 2025-10-28 20:58:22 UTC
Thanks for the bug report and the debugging.

I'd like to get a minimal reproduction case for just the symptoms. Is there a way to reproduce this with, for example, KWrite or Kate? I'm seeing key repeat work as expected with them, even when copying (adding to the clipboard)
Comment 2 gbqofoui 2025-10-28 21:53:20 UTC
(In reply to TraceyC from comment #1)
> Thanks for the bug report and the debugging.
> 
> I'd like to get a minimal reproduction case for just the symptoms. Is there
> a way to reproduce this with, for example, KWrite or Kate? I'm seeing key
> repeat work as expected with them, even when copying (adding to the
> clipboard)

I'm not sure exactly what the problem is here but I think it's to do with the interaction between key repeat and the clipboard (wl-clipboard) which non-vi editors won't have. Regular vim behaves exactly the same as neovim (`vim -u NONE` followed by `set clipboard=unnamedplus`). I've also observed a change in the key repeat behaviour in vscode using the vscode-neovim plugin where keys that interact with the clipboard (eg holding x or p) do repeat but at a lower frequency than keys that don't interact with the clipboard (eg j). vscode with the emulated vim plugin is not affected.

With wl-clipboard uninstalled vim and neovim no longer have the key repeat issue but shows an error that no clipboard provider is installed so I'm pretty sure it's delegating clipboard interactions to that, which is momentarily stealing focus. I'm not sure if this is a bug with wl-clipboard or the clipboard portal.

I tried Kate with vi mode and it doesn't have any issue, but that is probably interacting with the clipboard in an entirely different way.
Comment 3 TraceyC 2025-10-28 22:09:54 UTC
Thanks for the extra details.

I'm able to reproduce your results on Plasma built from git-master, using the steps in your first message.
Comment 4 gbqofoui 2025-10-28 22:18:22 UTC
I created a shim to see what arguments vim was calling wl-copy with. seems to be `wl-copy --type plain/text` with the content to be copied written to stdin. I tried to create a minimal example with PyQt calling out to wl-copy and seeing if focus is lost but I didn't see any 'focus gained' or 'focus lost' messages.

```
#!/usr/bin/env -S uv run --script
# /// script
# dependencies = [ "PyQt6" ]
# ///

import subprocess

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QWidget


class FocusLogger(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(300, 200)
        self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
        print("Start")

    def focusInEvent(self, event):
        print("Focus gained")
        event.accept()

    def focusOutEvent(self, event):
        print("Focus lost")
        event.accept()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key.Key_Space:
            print('Copy')
            subprocess.run(["wl-copy", "--type", "text/plain"], input=b"foo", check=False)
        event.accept()

if __name__ == "__main__":
    app = QApplication([])
    window = FocusLogger()
    window.show()
    app.exec()
```
Comment 5 ilay-JBR 2025-10-31 13:24:32 UTC
I also confirm this bug
It's making working with yakuake/kitty-quick-terminal impossible, and is ruining my whole workflow
Comment 6 Zamundaaa 2025-10-31 15:30:14 UTC
This is a bug in wl-clipboard, it's doing weird things. See https://github.com/bugaevc/wl-clipboard/issues/268 for more details
Comment 7 ilay-JBR 2025-10-31 16:55:50 UTC
(In reply to Zamundaaa from comment #6)
> This is a bug in wl-clipboard, it's doing weird things. See
> https://github.com/bugaevc/wl-clipboard/issues/268 for more details

Yes, it seems so
Thank you
Comment 8 gbqofoui 2025-10-31 21:23:52 UTC
I can confirm that replacing wl-clipboard with wl-clipboard-git the issue is resolved for me.

To summarize https://github.com/bugaevc/wl-clipboard/issues/268

Plasma removed support for the `wlr-data-control` protocol and wl-clipboard only switched over to the replacement (`ext-data-control`) in https://github.com/bugaevc/wl-clipboard/pull/255 which is not part of any release yet.
So this should be resolved when wl-clipboard makes a new release.