Software Engineer's Blog

KDE Plasma Wayland: Bind Middle-Click to a Region Screenshot

KDE Plasma Wayland: Bind Middle-Click to a Region Screenshot

On Windows I used PicPick, and one of the things I liked was binding a mouse button to a region capture. After moving to Kubuntu I never found a screenshot tool I liked as much. I didn’t end up installing one: on KDE Plasma Wayland, KWin and Spectacle can turn middle-click into a region screenshot, as long as you skip System Settings and write one line to kcminputrc.

Result: middle-click → drag a box with the left button → release, and the screenshot is taken. Environment: Kubuntu, KDE Plasma 6 (Wayland), KWin 6.6.7. You need a KWin with MR 5959, committed in September 2024. The commit first shipped in the v6.3.0 tag, so Plasma 6.3 or later.

Why System Settings won’t bind the middle button

In System Settings → Mouse → “Configure extra buttons”, I clicked Add Binding and pressed the middle button. Nothing happened. The capture dialog filters out the left, right, and middle buttons on purpose, so you can’t rebind one and lose a basic click. KDE bug 475879 is closed as RESOLVED INTENTIONAL for that reason.

The backend is a different story. Before MR 5959, KWin’s button rebind filter only took extra buttons (KDE Discuss thread). Since MR 5959 it accepts any mouse button, but the settings page didn’t change, so only the config file can reach the feature.

I didn’t want to guess the key names, so I read buttonrebindsfilter.cpp at tag v6.6.7, the version I run. The key is a Qt MouseButton name and the value is Key, followed by QKeySequence text. The whole change is one entry in ~/.config/kcminputrc:

[ButtonRebinds][Mouse]
MiddleButton=Key,Meta+Shift+Print

Set it up

1. Make the middle button send the region-capture shortcut

kwriteconfig6 --file kcminputrc --group ButtonRebinds --group Mouse --key MiddleButton --notify "Key,Meta+Shift+Print"

Meta+Shift+Print is Spectacle’s default shortcut for “Capture Rectangular Region”, so middle-click opens the region selector directly. If you’ve changed that shortcut, check System Settings → Shortcuts → Spectacle and use yours.

2. Capture on release instead of waiting for Enter

kwriteconfig6 --file spectaclerc --group General --key useReleaseToCapture true

By default Spectacle waits for Enter after you draw the box. This key is the same setting as “Accept on click-and-release” in Spectacle’s settings. I found the key name by running strings on the installed spectacle binary.

3. Reboot

KWin reads this config once at startup and rereads it only on a change notification (KConfigWatcher). On my machine, --notify still wasn’t enough: middle-click kept pasting until I rebooted, and a reboot worked every time. I didn’t dig into why the notification failed.

Spectacle is different. It reads its config each time it runs, so step 2 applies without a reboot.

Check it works

Middle-click, and the region selector opens. Drag with the left button, let go, and the screenshot is taken.

If you paste these screenshots into a terminal tool like Claude Code and nothing happens, that’s a separate Wayland clipboard issue: see When Claude Code won’t paste images on Linux (Wayland).

Troubleshooting

SymptomCause and fix
Middle-click still pastesKWin hasn’t reloaded the config. Reboot.
Spectacle still waits for Enter after you let goProbably an old Spectacle instance is still running. Quit it from the tray, or turn on “Accept on click-and-release” in Spectacle’s settings.
Holding the middle button and dragging does nothingExpected. KWin turns the press into a key event, and the selector opens after that, so it never sees the drag. Drag with the left button.
KWin crashed after the rebootCheck the stack before you blame the config. Mine crashed once; coredumpctl showed the stack in Mesa’s libgallium (the GPU driver), with no rebind code in it.

What you give up

Middle-click paste (primary selection) and opening links in a new tab.

Undo

kwriteconfig6 --file kcminputrc --group ButtonRebinds --group Mouse --key MiddleButton --delete

Then reboot.

On an older KWin

If your KWin predates MR 5959, or you’d rather use a GUI, input-remapper is the usual tool for mapping mouse buttons to keys on Wayland (Arch Wiki). I didn’t install it because KWin was enough.

Sources