Single Monitor - Virtual Desktop Switching.

Hello, im pretty new to Linux. Currently been using Cachyos for a few month’s and it has fully hooked me in moving from Windows. Really enjoying the freedom and the way im encouraged to make the experience my own.

That been said, I read about the Virtual desktop changes with the latest 6.7 update and couldn’t wait. I love the idea of being able to play my games and be able to switch desktops on a separate monitor without leaving my game.
The Individual virtual desktop per monitor really appealed and ive been trying to set this up.
Now the premise is working, but ive reached a sticking point that I cant figure out or find any information in regards to a solution to my problem.

So, im running a 3 monitor setup. Id like to like mentioned keep focus and ensure my mouse stays within my game monitor. But be able to use a short cut (Meta F1 or a Stream Deck / Using opendeck button) to switch desktops on my side monitor.

Basically on my side monitor i have discord on one desktops Game notes/guides on the other. So switching is really nice.

Sadly at this moment, pressing the shortcut swaps the desktop on my current monitor (or mouse focused)
So its not ideal to my exact needs. (Yeah i know im been a bit picky)

I managed to make a work around via opendeck.
Making an Multi Action button.
Bind a set key in KDE to “Switch to desktop 1”
And then run “qdbus6 org.kde.KWin /KWin nextDesktop”

This works for the most part, but does take my game window out of focus for a split second, which is ok for 95% of the time. Sadly I noticed in some games it did make the game crash/camera freak out.
Only happened a few times in the past 24 hours, but its enough for me to wonder if anyone with more experience/understanding knows a better way?

Sorry, i maybe not clear with what im asking and it might not be possible. But im just wondering :smiley:

Many thanks

Adam

I managed to find a soloution with the help of chatgpt

My setup:

  • Plasma 6.7.1

  • Wayland

  • 3 monitors

  • DP-2 = game monitor

  • HDMI-A-2 = side monitor

The built-in desktop shortcuts operate on the monitor containing the mouse cursor, which wasn’t suitable while gaming.

After some experimentation with the KWin scripting API, I found that Plasma 6.7 exposes:

  • workspace.currentDesktopForScreen(...)

  • workspace.setCurrentDesktopForScreen(...)

Using those, I created a small KWin script that registers shortcuts to switch desktops only on a specific monitor.

The monitor name is hardcoded as:

const TARGET_SCREEN = "HDMI-A-2";

Replace that with your own output name.

Tested successfully on Plasma 6.7.1 Wayland.

For anyone wanting the script to try (main.js)

const TARGET_SCREEN = “HDMI-A-2”;

function findTargetScreen() {
for (let i = 0; i < workspace.screens.length; i++) {
if (workspace.screens[i].name === TARGET_SCREEN) {
return workspace.screens[i];
}
}
return null;
}

function desktopIndex(desktop) {
for (let i = 0; i < workspace.desktops.length; i++) {
if (workspace.desktops[i].id === desktop.id) {
return i;
}
}
return 0;
}

function setDesktopForScreen(screen, desktop) {
try {
workspace.setCurrentDesktopForScreen(desktop, screen);
return true;
} catch (e) {
print("desktop, screen failed: " + e);
}

try {
    workspace.setCurrentDesktopForScreen(screen, desktop);
    return true;
} catch (e) {
    print("screen, desktop failed: " + e);
}

return false;

}

function switchSideMonitor(direction) {
let screen = findTargetScreen();

if (!screen) {
    print("Could not find target screen: " + TARGET_SCREEN);
    return;
}

let current = workspace.currentDesktopForScreen(screen);
let currentIndex = desktopIndex(current);
let count = workspace.desktops.length;

let nextIndex = currentIndex + direction;

if (nextIndex >= count) {
    nextIndex = 0;
}

if (nextIndex < 0) {
    nextIndex = count - 1;
}

let nextDesktop = workspace.desktops[nextIndex];

print("Switching " + TARGET_SCREEN +
" from " + current.name +
" to " + nextDesktop.name);

setDesktopForScreen(screen, nextDesktop);

}

registerShortcut(
“HDMI-A-2 Next Desktop”,
“HDMI-A-2 Next Desktop”,
“”,
function () {
switchSideMonitor(1);
}
);

registerShortcut(
“HDMI-A-2 Previous Desktop”,
“HDMI-A-2 Previous Desktop”,
“”,
function () {
switchSideMonitor(-1);
}
);

print(“HDMI-A-2 desktop switcher loaded”);

Shortcuts are in Keyboard
Shortcuts can be made in Settings > Keyboard > Search HDMI-A-2

HDMI-A-2 Next Desktop
HDMI-A-2 Previous Desktop

For anyone with an streamdeck using opendeck. Make a run script

// Next Desktop
qdbus6 org.kde.kglobalaccel /component/kwin org.kde.kglobalaccel.Component.invokeShortcut “HDMI-A-2 Next Desktop”

// Previous Desktop
qdbus6 org.kde.kglobalaccel /component/kwin org.kde.kglobalaccel.Component.invokeShortcut “HDMI-A-2 Previous Desktop”

Hope this can help someone else :slight_smile:

1 Like