You are not logged in.

#1 2022-12-22 13:46:42

millus
Member
Registered: 2019-07-21
Posts: 193

How to send keystrokes to background window?

The important part here is that the window must not be brought to the foreground, or I could just raise it with xdotool.
I have disabled the compositor as it is known to interfere/cause problems (I never enable it on any box).
On some older stackoverflow sites it is suggested to use

xdotool type --window id $text

but I found that this "--window id" thing does not work at all, at least on my arch x64 / xfce4 pc, not even with a simple terminal window.
In fact, if I use xdotool with --window param it will not output anything at all, anywhere, really weird.
What does work, however, is sending a "click 1" event to another window. Just xdotool's "type", "key", "keyup" and "keydown" commands do not work.

Then there is python's pywinauto, but it only really supports Windows so far it seems.

I also tried python-ldtp but this only supports python 2. On my box I have python 3 so its command-line binary "ldtp" doesn't even work but just throws a lot of errors that are unsolved unless you switch to python2 apparently. I'm very hesitant to install python2 alongside python3 because I'm not sure if this will cause any internal trouble that is hard to resolve, like weird bugs where you have no idea why they suddenly start happening, if you mix both versions on one pc.

So it'd be ideal to get xdotool with --window id to work somehow hmm I couldn't find anything so far though.

Anyone know if this can be done in C with the X11 library somehow maybe?
More edit:
I tried to use X11 lib's "XSendEvent", using a mousepad instance or a terminal window id as window handle, and it sends exactly nothing apparently. Why is everything broken? :-p
Yeah, apparently XSendEvent doesn't work at all. What works is XTestFakeKeyEvent from XTest extension of X11 lib. Prob is tho, this function does NOT accept a target window, lol. So it will always post to the currently active window. Geez, why is this SO HARD to do something SO SIMPLE??? Some huge underlying design issues perhaps?

Thanks

Last edited by millus (2022-12-23 02:05:25)

Offline

#2 2022-12-23 04:12:08

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,130

Re: How to send keystrokes to background window?

Fwiw, unless you installed Arch very recently, you likely had python2 installed alongside python(3) until fairly recently, too. They certainly coexisted in the repos and on many machines for a considerable time.


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#3 2022-12-23 11:03:20

seth
Member
Registered: 2012-09-03
Posts: 49,992

Re: How to send keystrokes to background window?

Xtest just puts an event on the server - it's then processed sas if it was coming from a real keyboard.
Many clients and certainly both big toolkits ignore synthetic events from xsendevent for security reasons (since at least a decade)

What are you actually trying to do?
Nb. That focus/active/foreground are different things and depending on your WM can be used independently (ie. Theoretically a window can have the focus w/o being in the foreground)

Offline

#4 2022-12-23 23:33:35

millus
Member
Registered: 2019-07-21
Posts: 193

Re: How to send keystrokes to background window?

There is a game (lutris/proton) running in the background on another monitor, its window doesn't have focus, and I want to check the scores in regular intervals, for this I have to press a key to bring up the scores screen.
So I want to send the keys in the background maybe every 30 seconds to switch between that scores screen and another 'normal' screen with other info.

Currently my script gives focus to the game every 30 s and then sends the key and then restores focus to the app I was previously using (usually web browser). But this is pretty annoying because it interrupts me every 30 s obviously. So I jsut want to send the keypress in the background without having to set focus to the game.

Last edited by millus (2022-12-24 01:05:20)

Offline

#5 2022-12-24 08:16:42

seth
Member
Registered: 2012-09-03
Posts: 49,992

Re: How to send keystrokes to background window?

wine filters xsendendevent events since ever, see eg. https://forum.winehq.org/viewtopic.php?t=27392 or https://forum.winehq.org/viewtopic.php?f=2&t=4135 or https://forum.winehq.org/viewtopic.php?t=16781
Apparently a lot of people run into this when trying to write bots lol

Can you simply script the game?
If not, according to one of the WinHQ threads, https://learn.microsoft.com/en-us/windo … endmessage should™ work and there're probably already some windows tools making use of it.

Offline

#6 2022-12-24 14:06:16

millus
Member
Registered: 2019-07-21
Posts: 193

Re: How to send keystrokes to background window?

^^ well, it doesn't work for simple mousepad or xfce4-terminal either, not just not for wine.
Not sure what you mean by "script the game".
Thanks for references, I will read that!
Would just love if there was a simple lowish level tool that operates with X11 or whatever is needed (I have not much clue of how X works) that just gives a program that is waiting for key input, a bloody key input, without asking here, filtering there, etc, JUST SEND A BLOODY KEY ON APPROPRIATE LOW LEVEL FOR GOD'S SAKE lol. I mean this seems like such a basic, essential and important/frequent use case it should "just be possible" (tm) on something as flexible as Linux, especially Archlinux, I don't get it sometimes why things are made so hard to work really.
I can understand if there are limitations to the user for security, but at the very least the root account should be able to do all of this simply if he wishes to, without having anything "filtered" wth.. imo this goes in the wrong direction heavily >_> linux should be about being in control of your system, not about your system filtering shits for you ^^.
Thanks for listening, I already feel a bit better.

Last edited by millus (2022-12-24 14:06:46)

Offline

#7 2022-12-24 14:19:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: How to send keystrokes to background window?

If these high scores are available in-game, I'd be they'd be listed somewhere online.  Wouldn't it be easier to find the actual source and just watch that webpage?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2022-12-24 14:42:50

seth
Member
Registered: 2012-09-03
Posts: 49,992

Re: How to send keystrokes to background window?

Not sure what you mean by "script the game".

If the game has a script engine, you could program *it* to show the scores every so many seconds.
This used to be a thing, but might have been limited to the widespread use of the ID tech engines.


it doesn't work for simple mousepad or xfce4-terminal either, not just not for wine

seth wrote:

certainly both big toolkits ignore synthetic events from xsendevent for security reasons (since at least a decade)

The both big toolkits are GTK & Qt. XSendEvent is basically useless for this purpose these days.

I mean this seems like such a basic, essential and important

… vector to attack the system?

at the very least the root account should be able

https://www.youtube.com/watch?v=5RyYrs5tu60
Also you should™ not run X11 clients as root at all.


In general, please stop ranting.
The decision on what to do w/ synthetic events is down to each client.
There's no systematic issue w/ that and it's certainly not related to arch or any linux distro. 20 years ago xsendevent would have justworked™ across the board (though indeed probably not w/ wine out of all clients)
It is that, in your case, wine, *specifically* does not *want* to process those events (whether for security reasons or maybe originally because the input mapping between the X11 and windows model is complex enough w/o) - it perfectly could but the authors have decided that that's not a good idea.

As mentioned, windows does have a similar function and I guess as long as the windows shortcut daemon and windows game run on the same wine server, you'll be able to send an input message this way.

Offline

#9 2022-12-24 21:39:57

millus
Member
Registered: 2019-07-21
Posts: 193

Re: How to send keystrokes to background window?

Thanks and sorry, didn't have time yet to check out everything cause of xmas.
But just wanted to say that in general I feel that clients should not even be able to discern "synth" events from "real" ones if I don't want them to be able to. Like, if I set some flag FAKE_EV_REAL for example. I guess I just don't like when I'm not given full control over my own system. When I pick up a screw driver or a hammer I don't expect them to let the screws/nails decide how to operate on them "in my bEsT iNtErEsT"... ^^ if this weird analogy maybe helps to understand what sentiment I'm trying to convey ^^.

Last edited by millus (2022-12-24 21:40:43)

Offline

#10 2022-12-24 21:48:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: How to send keystrokes to background window?

Feel free to write your own X11 server and it will do whatever you want.  When you rely on software, or hand tools, made by someone else, you may be limited by their preferences / assumptions.

When you pick up a screw driver, you dont have complete control of how it will work either.  You might find you are holding a phillips head screwdriver and looking at flat head screws - in which case you'll be in the same perdicament: you want the tool to do something it was explicitly designed not to do.  You can, of course, go buy a different tool that will do what you need in that moment.  But it'd be pretty silly to complain to the manufacturer of the phillips head screwdriver that it doesn't work on flat head screws.  It'd be even more silly to complain about that mismatch on a carpentry forum expecting experienced carpenters to care.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2022-12-25 00:57:46

millus
Member
Registered: 2019-07-21
Posts: 193

Re: How to send keystrokes to background window?

Yeah, but if I want to try and hit a nail with the screw driver (assuming the handle isn't made form weak plastic), but the screw driver sends a radio signal that commands the nails to bend out of the way when I try to hit them with the screwdriver, then I would complain (or try to patch it by microwaving it for a moment). However, "complain" is a harsh word, I was just baffled about the rocks in my way, in this key-sending case, is all. If you felt it was complaining than at the restrictive status quo itself that breaks your freedom of operation, not at the works/effort of anyone.
What you describe is more like, I am unhappy because I cannot somehow send a SIGKILL signal with a tool that sends keydown events.
Anyway I shouldn't have started with analogies I guess, it feels like it derails the thread from its informative nature to a philosophical discussion ~_~.

Last edited by millus (2022-12-25 01:37:18)

Offline

#12 2022-12-25 19:06:19

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,130

Re: How to send keystrokes to background window?

If you microwave your screwdriver for a minute, you'll destroy your microwave.


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

Board footer

Powered by FluxBB