News for hackers. Stuff that matters.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by chaorace on Monday May 17, 2021 @10:53AM (#27184132)
I am now curious, though. How does Wayland handle key events? I have to imagine it only lets the active window listen in on key events, right? Maybe there's an API for subsribing to specific key combinations?
Re:Why Is It So Hard to Detect Keyup Event on Linux? by ashafer on Monday May 17, 2021 @11:22AM (#27184470)
iirc it's pretty much completely up to the compositor. The spec is worded to allow "focus" to enter a surface and deliver events to it, but it never specifies under what conditions the compositor should do so.

So the compositor chooses what surface to deliver events to based on its own desires (like letting pointer focus enter background apps) and the users input. I think there is a protocol (used by Xwayland?) to allow a client to get events from any window if the compositor/user allows it

Re:Why Is It So Hard to Detect Keyup Event on Linux? by Cloudef on Monday May 17, 2021 @12:12PM (#27185104)
That's how it is. It's up to compositor to decide what events to send to what clients. Usually this means compositor only forwards input events to the focused surface.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by chrisseaton on Monday May 17, 2021 @11:02AM (#27184244)
> Maybe there's an API for subsribing to specific key combinations?

Why would you ever let a non-focused application subscribe to any key combinations?

Re:Why Is It So Hard to Detect Keyup Event on Linux? by reificator on Monday May 17, 2021 @11:17AM (#27184412)
> Why would you ever let a non-focused application subscribe to any key combinations?

* Media playback controls

* Hotkeys to take screnshots, or record videos

* Quake style terminals

* Ingame chat and game invites

* Color pickers

* Screen controls like F.lux

* Window management hotkeys

* Push-to-Talk in group voice calls

And that's just what I personally use on my own machine.

It's a massive security issue that any installed software can listen to any input activity or view/affect other windows, and that does need to be reigned in. But to claim that there's no legitimate utility to global hotkeys is absurd. We need robust permissions, not a completely crippled experience.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by icedchai on Monday May 17, 2021 @06:32PM (#27189221)
It doesn't even need to be installed. Back in the 90's, some Unix workstations shipped with X11 security disabled by default. Your keyboard could be sniffed just by plugging into the network. Fun times.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by topaz0 on Monday May 17, 2021 @12:21PM (#27185208)
In my experience most of these use cases are handled on linux by the window manager calling a script that sends a message to the relevant application, e.g. over dbus.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by reificator on Monday May 17, 2021 @12:33PM (#27185322)
Sure, and that works fine. Just let my application tell the window manager:

* What key events I care about. (I.E. what the keybind should be named in the settings UI)

* What script or message to pass back to me when that event is activated.

* What the default keycombo should be, if any.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by michaelmrose on Monday May 17, 2021 @02:45PM (#27186710)
Some applications prefer to provide a ui for the user to configure a hotkey. This is naturally discoverable within the scope of the other settings in its preferences screen.

Providing a cli interface for automation or binding a hotkey is imo more powerful and useful but its not discoverable. Doubly so for a controlling it over dbus.

The logical thing is that since there is a substantial use case and a desire to limit applications access to global info is that a permission system ought to have been built into Wayland such that applications could request not only global access to the keyboard but permission to get a notification when a particular key press happened.

Since this feature was a staple of desktop operating systems for decades it ought to have been part of the plan from the start.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by zxzax on Monday May 17, 2021 @04:23PM (#27187984)
People have been asking for similar grab-type things since early on in Wayland, and there have been many proposals, but nobody has actually stepped forward to build such a permission system that would work everywhere and would not create additional security problems or would not severely overcomplicate things. It's not a simple thing to do, by any means. If you think you know how, I would urge you to get started designing it and contributing it to some of the major Wayland implementations.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by michaelmrose on Monday May 17, 2021 @05:30PM (#27188699)
I don't really want to be in the business of growing wheat or baking bread if what I actually want is a sandwich but I will happily explain why ___ sandwich shack is doing it wrong because Monday morning quarterbacking is more fun.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by zxzax on Monday May 17, 2021 @05:33PM (#27188725)
I get that, but everyone else also seems to have that attitude, so you see why it doesn't really get done when nobody has any wheat or bread :)
Re:Why Is It So Hard to Detect Keyup Event on Linux? by pjc50 on Monday May 17, 2021 @11:06AM (#27184295)
Re:Why Is It So Hard to Detect Keyup Event on Linux? by chrisseaton on Monday May 17, 2021 @11:10AM (#27184338)
That doesn't seem like something that an application should be getting involved with. An application should only be able to manage its own windows and key presses to its own window.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by reificator on Monday May 17, 2021 @11:19AM (#27184434)
So if I'm in my text editor and decide I want to change the currently playing song, I need to switch windows/contexts, press a hotkey or GUI button, and switch back?

The current model where everything running can listen to everything you do is not acceptable, but that doesn't mean there's no usecases that strongly benefit from global hotkeys.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by chrisseaton on Monday May 17, 2021 @11:33AM (#27184611)
> So if I'm in my text editor and decide I want to change the currently playing song, I need to switch windows/contexts, press a hotkey or GUI button, and switch back?

Looks to me like something the DE should manage for you, not the application.

But everyone seems to disagree with me!

Re:Why Is It So Hard to Detect Keyup Event on Linux? by TeMPOraL on Monday May 17, 2021 @11:39AM (#27184704)
But how do you teach your DE to do new things for you? And what is your DE in the first place?

The answer to both these questions is: an application. Think of applications hooking into global shortcuts as plugins for your DE.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by reificator on Monday May 17, 2021 @11:45AM (#27184768)
> Looks to me like something the DE should manage for you, not the application.

> But everyone seems to disagree with me!

The original post you responded to was suggesting that your DE, or at least something outside of your application, provide an API to register specific global hotkeys that you can listen for. In response, you said:

> Why would you ever let a non-focused application subscribe to any key combinations?

So to me when reading this thread, it looks like you're the one disagreeing with this idea. This is the first post of yours I've seen where you suggest this, and the thread begins with you shooting down the entire concept as ridiculous.

Unless you're suggesting that the DE comes with a preconfigured set of global hotkeys and cannot be altered or extended, nor handled differently by different applications. In which case yes, I strongly disagree with that.

There's a reasonable middleground between a free-for-all and nothing but what the DE already thought of, and that's a well-defined API boundary alongside per-application, per-feature permissions. Bonus points if the DE handles all key-combination assignment in a consistent UI and applications can only register a suggested keycombo, a context for when the hotkey should be activated, and a function to respond to the event.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by chrisseaton on Monday May 17, 2021 @11:59AM (#27184947)
> Unless you're suggesting that the DE comes with a preconfigured set of global hotkeys and cannot be altered or extended

Yes that's what I'm suggesting. Works fine for almost all use-cases like on macOS.

I guess it's just not a popular opinion lol!

Re:Why Is It So Hard to Detect Keyup Event on Linux? by eropple on Monday May 17, 2021 @12:12PM (#27185112)
I've had custom global hotkeys for stuff in MacOS for over a decade.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by chrisseaton on Monday May 17, 2021 @12:13PM (#27185124)
But that's not how things like playback controls are implemented - they have non-extensible separate APIs for that. That's the point.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by eropple on Monday May 17, 2021 @12:30PM (#27185288)
You've also been given a raft of other functions that depend on them in this thread.

I'm not sure what your hangup here actually is.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by zxzax on Monday May 17, 2021 @04:34PM (#27188105)
IME, most of those functions tend to get implemented in the DE when it turns out they're actually useful, or some other mechanism can be used to activate them. So no reason to get hung up on that particular method to do it either.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by reificator on Monday May 17, 2021 @12:16PM (#27185164)
I did edit the post you responsed to a few times in the last few minutes, so heads up there.

> > Unless you're suggesting that the DE comes with a preconfigured set of global hotkeys and cannot be altered or extended

> Yes that's what I'm suggesting.

Okay, let's use the example of music playback. If your DE handles music playback controls, how does it tell the music player when to stop and start, or skip to next track?

Does it hardcode a list of music players, each of which provides its own bespoke API, and the DE calls into the application?

Or does it provide a way for any music player to call into the DE, and listen for those play/pause/skip events?

And if it does that, then why are the list of events hardcoded? Why not allow the music player to say "I also play podcasts, and I want to provide a hotkey to speed up/slow down the playback speed, or provide a separate hotkey to skip ahead 30 seconds that's distinct from skipping the entire episode."? Why not allow Discord to provide a push-to-talk hotkey so that people on a group call can actually hear each other without dogs barking and keys clacking and people coughing?

---

Look at the VSCode Extension API for instance:

* Extensions are at least partially isolated from the main process and each other. I don't know how far this goes or how secure, but for our purposes they could be as isolated as you want.

* They can register a command which when activated, performs some action from within the extension.

* Those commands show up in the command list at the top of the screen when you hit "Control + Shift + P" (which is itself a command subject to all the same rules) and users can select whichever command they want to run.

* Those commands can also be assigned to a hotkey.

* Extensions can suggest a default binding for this hotkey, but users can go into the VSCode settings page and assign whatever keycombo they desire.

* Users can remove keybindings for commands that have them by default.

* Users can assign keybindings to commands that don't have them by default.

* All keybind editing is performed through the same UI, which is owned by VSCode and not the extension.

* VSCode itself is the thing listening to those key events, and it calls into the extension to activate it.

* There is a concept of context for when these keybinds apply, such as when a markdown document is active in the editor. Extensions provide these contexts by default and users can override them as they choose.

* Extensions themselves don't even start up until an activation event is met. These can be opening a file of a certain language, running a command, etc...

What about this model is objectionable in your mind? Who benefits from the lockdown you propose where no new ideas can be tried until the DE authority deigns to allow it?

Re:Why Is It So Hard to Detect Keyup Event on Linux? by zxzax on Monday May 17, 2021 @04:05PM (#27187777)
All of your concerns regarding music players are actually already addressed by the MPRIS spec: https://specifications.freedesktop.org/mpris-spec/latest/

This problem has been solved in the DEs for quite some time now, global hotkeys is probably the very last thing you want to reach for, when all other options are exhausted.

And when that happens, I don't know about other DEs, but the way you're describing with extensions is mostly how GNOME Wayland already works. If you want to intercept a key, you ship an extension for that, and display a UI to rebind the keys, or you can place additional entries in the system's keyboard settings panel. What you can't do is have random unprivileged processes intercept keys without the user's permission.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by michaelmrose on Monday May 17, 2021 @02:47PM (#27186736)
Probably because nobody on earth wants what would be the convenient design to dictate how they use something.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by TeMPOraL on Monday May 17, 2021 @11:19AM (#27184442)
Depends on an application. Plenty of applications have a legitimate reason to hook into global keyboard handlers, or otherwise instrument other applications or the system itself. Removing that capability would be crippling to the utility of a computer.

It sort of works in the mobile space (+/- accessibility services, which are the way to get that functionality if you need it), but that's only because mobile devices are consumption-oriented; there's only so much you can do with them, and they eschew utility in order to streamline you into being monetized by third-party services.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by R0b0t1 on Monday May 17, 2021 @11:15AM (#27184393)
Problematic. Means you are relying on the DE for all UI development. With Windows and X11 the DE is reconfigurable to some extent by applications.

There is also more to this, showing the Wayland security model is incoherent. If debugging is enabled on your system I can simply debug your root terminal and start injecting commands into memory.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by zxzax on Monday May 17, 2021 @04:21PM (#27187959)
Wayland obviously cannot solve all security issues and privilege escalations in the underlying OS -- the solution to that problem would be to just disable debugging within that security context.

In Wayland the situation is also not so much different from Windows, you would just reconfigure and extend the DE itself. You are already relying on them to provide most of the UI to an extent.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by R0b0t1 on Monday May 17, 2021 @05:25PM (#27188640)
In regards to your first point, I am saying that Wayland does not do what it sets out to do and the proper way of doing it is a much larger problem than what Wayland can address. It is possibly better to avoid breaking everyone's stuff if you're just going to need to shortly do it again to fix the things Wayland does not and can not fix.

For your latter point, Wayland is much different. In Windows there are ways to inject behavior into other programs or the UI elements. This exists so you can change the UI. Wayland's idea of security is very much against that, at least in the way it can be done on Windows/X11.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by zxzax on Monday May 17, 2021 @05:38PM (#27188769)
I don't know what you mean you'll need to break it again, this one particular hole is already plugged in Wayland. That won't cause additional breakage, and you'll still want to be plugging those additional holes like ptrace anyway.

Input injection is being worked on in a different library, that brings a standard API that's supposed to work the same across Wayland, X11, and with sandboxed contexts: https://gitlab.freedesktop.org/libinput/libei

Re:Why Is It So Hard to Detect Keyup Event on Linux? by simion314 on Monday May 17, 2021 @11:26AM (#27184528)
Not all applications need to be a classic "windowed application".

I know this is a rare use case, but the DE and the app need to work togheter to allow global shortcuts. I for example have a keyboard with many useless media keys , that I configured to run some specific scripts and some I could very easy set them up as Global shortcuts for some KDE apps. Not sure if other DEs would offer to setup global shortcuts for you.

Probably something most users will use is screen recordings and screen readers , this applications need to have global shortcuts(and access to the screen and window elements)

Re:Why Is It So Hard to Detect Keyup Event on Linux? by pdkl95 on Monday May 17, 2021 @12:16PM (#27185160)
I am currently writing a utility to spy on Super Hexagon's KeyPress/KeyRelease XEvents with the goal of recording very precise timestamps for each event. Since the walls in the game arrive at a constant rate, I want to graph my keypress/keyrelease events modulo the time between walls.

My success rate for finishing Hyper Hexagonest[1] is falling; being able to inspect recent attempts might reveal where my timing is drifting.

(it's only ~25%-ish finished, unfortunately)

[1] https://www.youtube.com/watch?v=JJ96olZr8DE

Re:Why Is It So Hard to Detect Keyup Event on Linux? by jeroenhd on Monday May 17, 2021 @11:10AM (#27184333)
WorkRave can't function well without it. Currently, it's working through a compatibility layer I think, but if that breaks, the application can't be made to work on Wayland AFAIK.

Alternative shortcut managers might also run into trouble (say, clipboard managers with customizable shortcuts) although those could work with a simple permission prompt for each new shortcut. Very annoying and obtrusive to the user, but the security principles remain.

There are also tools that extend some window managers with i3-like shortcuts and configurations; the config files are parsed on the fly and they need to work in the background.

Then there's tools like AutoHotkey that can be great tools for productivity through scripting and custom shortcuts. A lot of AH's functionality can be replicated using the standard hotkey API, but not everything.

There's also diagnostic tools (see Windows Steps Recorder) that record all keystrokes and generate a step by step report about what happened with screenshots as a guide.

IMO global key capture should still be possible with the right capabilities set because there are valid use cases for it. Requiring additional permissions is fine IMO, but completely removing the option to do this is a pain.

Re:Why Is It So Hard to Detect Keyup Event on Linux? by yjftsjthsd-h on Monday May 17, 2021 @11:09AM (#27184330)
In order to implement https://www.semicomplete.com/projects/keynav/ (or anything else that listens for a specific key combo to activate)
Re:Why Is It So Hard to Detect Keyup Event on Linux? by chrisseaton on Monday May 17, 2021 @12:08PM (#27185048)
Isn't that kind of thing the responsibility of the desktop environment service? Not an application?
Re:Why Is It So Hard to Detect Keyup Event on Linux? by riskable on Monday May 17, 2021 @02:26PM (#27186471)
As long as the desktop environment provides a mechanism for applications to register their global shortcuts then all is well. However, without such a feature the application has to just listen for all keystrokes and check if it matches something the user's configured.

Example: I've bound several scenes in OBS to keyboard shortcuts so that I can switch scenes no matter what application I'm in. I'm pretty sure OBS does this by listening to all keyboard events on the root window. It's not very efficient, but it works.

Now imagine having to support a global shortcut daemon per DE that exists. I suppose someone would write an abstraction but really, it's something that FreeDesktop.org should provide (if it already doesn't).

Re:Why Is It So Hard to Detect Keyup Event on Linux? by yjftsjthsd-h on Monday May 17, 2021 @01:34PM (#27185937)
I suppose the proper answer is that that very much depends on your philosophy of desktop environments; I happen to prefer modular desktops built out of separate programs from separate authors which may or may not even know about each other. This has the advantage of making it trivial to retrofit new features without needing to ask your DE upstream. Or to be more pithy: Okay, fine; what desktop environments implement keynav? With X11, I can make the answer be "(probably) all of them".
Re:Why Is It So Hard to Detect Keyup Event on Linux? by joaonmatos on Monday May 17, 2021 @11:10AM (#27184334)
Global Hotkeys for something like Quake terminal, or voice chat push-to-talk. Obviously I don't see why you would encumber the core protocol for it, but a subscription model for it as a wlroots or FreeDesktop portal could be interesting.
Re:Why Is It So Hard to Detect Keyup Event on Linux? by tjalfi on Monday May 17, 2021 @01:37PM (#27185975)
PureText[0] is a Windows application I use regularly. It adds a keyboard shortcut for pasting as plain text.

[0] https://stevemiller.net/puretext/