How to simulate a key press in C++ for Linux?

Red Squirrel

No Lifer
May 24, 2003
67,308
12,093
126
www.anyf.ca
I've written a simple program in windows before to simulate hitting the scroll lock key. Running it prevents the screensaver from coming up, in situations where it can't be disabled.

I need to do the same in Linux. Raspbery PI screensaver is very hard to completely disable, and even when you manage to disable it, every now and then it just seems to come back. I just want to have the app run at startup or something, and it will hit scroll lock every 30 seconds or so. I just can't seem to find much info on how to do that. In Windows I'd just use win32 api.

If it turns out to be too complicated I'll see if I can figure out a hardware solution using an atmega8 or something.
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
It is unfortunately not simple.

Unlike windows, linux has multiple gui systems (most based on X). Further, X decided to make everything complex by introducing a Client/server aspect to the way things are handled. Nice because it gives you remote X basically for free. Not so nice because it makes everything a little more complex.

First step, figure out which windowing system your OS flavor uses (Xorg, XFree86, Wayland?).

Second step. Google: "Send keystroke *X flavor*"

third step, cry

forth step, dive into the man pages for various esoteric methods and incantations.

Expect nothing less than ~20 lines of code to do it right :)


Good luck!
 

Red Squirrel

No Lifer
May 24, 2003
67,308
12,093
126
www.anyf.ca
Wow sounds like a pain. Not even sure what the window manager is, OS is Raspbian so it's probably rather custom. Might be easier to just explore the hardware solution right off the bat.
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
Raspbian uses Xorg.

That is where I'd start digging "Send keys Xorg"