Skip to main content


Anyone familiar with #x11 could give me a hand with something?

I'm trying to send a key event to a previously focused window that is not itself.

For example, you focus on firefox, then open a terminal window and run this, the event is sent to firefox, and not itself.

https://paste.sr.ht/~rabbits/c6e817d573b0ffcc26cfa76d4fd602513a9386c9

#x11
in reply to Devine Lu Linvega

i don't in general see how the program could know what "the previously focused window" is. I don't think X keeps track of it. Do you have other means of knowing it? (if you do, it seems like you could modify xfake.c to send an event to any particular window; X definitely supports that).
in reply to David JONES

@drj I'm not sure, is there a way I could send events to a particular window by recording which window I could out to, from my program? Like, if I loose focus to focus on a second window, is there a way to know that window's id?
in reply to Devine Lu Linvega

a) what are you up to?; b) i'm not sure; c) any X program knows the IDs of any windows it creates, and it is free to tell anyone (you, me, another program) about those IDs. And given an ID (they are 32-bit numbers i think) any program can now send events to that window. I think xterm for example advertises its Window ID in its env so that cli programs can, in principle, do X things to their containing xterm.; d) i don't use X any more so i can't poke around easily.
in reply to David JONES

my goal is to make an on-screen keyboard in Uxn, it's something I'd use daily.
The uxn emulator I have is written in x11 itself, but I would like not to have to modify the emulator, ideally I'd use a small intermediate program, like:

uxn11 keyboard.rom | some_little_program

The keyboard rom will send stdout events, and some_little_program will be the bit that handles the logic of which keycode to send, and which window to send it to

This entry was edited (9 months ago)
in reply to Devine Lu Linvega

so some_little_program needs to know the X window ID to send events to. Some ideas: you could just type it in; some_little_program could observe what window has focus at the moment it starts and use that one forever; one of the programs (I'm not sure which one) could make you click on the target window and listen for the event and use the target of the click for all future events.
in reply to David JONES

@drj Oooh! Yes, okay, let say I wanted to make it so some_little_program waits for a click on a window(`click on any window`), do you know how I can do this?
in reply to Devine Lu Linvega

I only ever wrote 1 X11 program, but it was using XLib and none of the new-fangled framework nonsense (it was on a DEC Ultrix machine in about 1994). I _think_ you "simply" call XNextEvent; see how xev.c does it https://gitlab.freedesktop.org/xorg/app/xev/-/blob/master/xev.c#L1479

I _think_ by default an X11 client gets all events (for all windows!) unless it has requested only events for particular windows (like their own, which most apps would do); i think that's XSelectInput.

in reply to David JONES

@drj aaah! amazing, thank you so much, that should be enough for me to get it working 😀
in reply to Devine Lu Linvega

@drj Not sure if this is helpful, but the `xprop` allows you to click on a window, and then it prints its properties to stdout. You could probably hook it up with a shell script, or maybe look into the source if you want to do something similar with the API
in reply to Devine Lu Linvega

I’d probably set up a global key event listener and then keep track of my windows on my own. Like this keylogger. Not sure if this is quite what you want though.

https://github.com/anko/xkbcat/blob/master/xkbcat.c

in reply to Devine Lu Linvega

X11 doesn't keep track of focus history, you would have to hook yourself to the FocusIn event and keep track yourself.
in reply to marmarta

So you mean, that when the window(ex, firefox) looses focus to focus on the new one(my program), I could record what that one is?
This entry was edited (9 months ago)
in reply to Devine Lu Linvega

I don't think core Xlib has any information about previously focused windows maintained in the display server, you'd likely have to have a client watching for focus change events a priori which could then know what was formerly focused.
in reply to Devine Lu Linvega

dunno if this is helpful, but I wanted a shell script to mute zoom, ended up with this:

xdotool windowactivate --sync $(xdotool search --name 'Zoom Meeting') key alt+a

which is roughly
1. find the identifier for the X window titled "Zoom Meeting"
2. send the key "Alt a" to that window

in reply to Devine Lu Linvega

You might look at xwininfo that can list all the windows on the display with their IDs, as a way of finding your target window (I don't mean using that program, just the way it works)
in reply to Devine Lu Linvega

I think you can use tab order/stacking order for this https://stackoverflow.com/questions/26175886/order-array-of-windows-by-z-order
in reply to timthelion

the order of the enumeration doesn't seem to change based on the z index of the window, it seems more like the order in the windows were created
This entry was edited (9 months ago)

Lo, thar be cookies on this site to keep track of your login. By clicking 'okay', you are CONSENTING to this.