Code: Select all
display = XOpenDisplay(0);
window = XDefaultRootWindow(display);
windowVentrilo = find_window(display, window, VENTRILO);
unsigned int simulatekey = XKeysymToKeycode(display, SIMULATEKEY);
...
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *) &event);
or
XSendEvent(event.display, event.window, True, KeyReleaseMask, (XEvent *) &event);
XFlush(display);Hang on, is that C or something? Just guessing that because of all the semicolons. I think I might keep this in mind in case I ever learn any C.cleverca22 wrote:http://gallery.earthtools.ca/download/ventctl.tar
i used this program many years ago to do something similar, it has 2 parts
first part opens /dev/input/x, and sniffs the keyboard for a hotkey like shift (remove this code)
the second part uses x11 api's to find the ventrilo window, and fake the A key being pushed or released (reuse this code)
that would work for any X11 (gui) app you wanted to poke keys at
its basicaly just thisoh, and this is the python forum, *facepalm*, could still be usefullCode: Select all
display = XOpenDisplay(0); window = XDefaultRootWindow(display); windowVentrilo = find_window(display, window, VENTRILO); unsigned int simulatekey = XKeysymToKeycode(display, SIMULATEKEY); ... XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *) &event); or XSendEvent(event.display, event.window, True, KeyReleaseMask, (XEvent *) &event); XFlush(display);
Code: Select all
import win32api#forget this and die
win32api.keybd_event(keycode, 0, 0, 0)#down
win32api.keybd_event(keycode, 0, 2, 0)#upThe best method depends not only on the operating system but also on the program to be fooled. For instance, spoofing X events to a specific window or storing input to a specific terminal is much safer than spoofing kernel input events. But each of these only works with the right type of application.Mario-Fan wrote:This only works for Windows, so I feel we should keep this thread open in case someone finds a way to do it on other osses (that word has to be a thing).
i can see it being usefull to turn the GPIO inputs into a keyboard (with a limited number of keys)jojopi wrote: Most importantly though, nobody ever wants to "press and hold a key, with no regard to what the user is doing at the time" (except perhaps if they are writing anti-social malware). Almost always they want to "do the thing that application Z will do for me, but the only way I know to tell it is to hold a key".
Since you seem to ask, I'm making a thing that basically does netplay on games where the only multiplayer mode is on the same machine. For lack of a better name, I'm calling it "Local Netplay."jojopi wrote:Mario-Fan wrote:Making some assumptions and concealing your real intention may get you a worse solution than asking your actual problem. Or none at all.
I just remembered that you can run C code in Python. So I can use this, but I have little to no knowledge of C (I can barely read basic programs since both Lua and Python are based off it) and don't really know what to do.cleverca22 wrote:http://gallery.earthtools.ca/download/ventctl.tar
i used this program many years ago to do something similar, it has 2 parts
first part opens /dev/input/x, and sniffs the keyboard for a hotkey like shift (remove this code)
the second part uses x11 api's to find the ventrilo window, and fake the A key being pushed or released (reuse this code)
that would work for any X11 (gui) app you wanted to poke keys at
its basicaly just thisoh, and this is the python forum, *facepalm*, could still be usefullCode: Select all
display = XOpenDisplay(0); window = XDefaultRootWindow(display); windowVentrilo = find_window(display, window, VENTRILO); unsigned int simulatekey = XKeysymToKeycode(display, SIMULATEKEY); ... XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *) &event); or XSendEvent(event.display, event.window, True, KeyReleaseMask, (XEvent *) &event); XFlush(display);
Supposedly all you have to do is import it, but I don't seem to be able to import the code right away. I think you need to #include <Python.h> first, according to this.cleverca22 wrote:how do you run c code in python?, if i had an example i could make the above code work in it
This will probably do the job, thanks.cleverca22 wrote:ive always been wanting to try out that part of python
https://github.com/cleverca22/xorg
the code in this repo will find any window that has 'Event' in the title bar (like xev, or just edit it to the name of your text editor for testing)
then it will simply send these keys, a, space, b, c, and backspace
each one is a separate press and release call, so you can hold it if you need to
most of the error checking is missing, and it doesn't send proper python exceptions, but it works
(i only tested it on my laptop, but it shouldnt be any different on a pi)

By the way, does this work on other Linux desktop environments like GNOME?cleverca22 wrote:yeah, its using Xorg headers, which simply wont exist on the windows pc, and if you did manage to compile it on windows, it would become a windows-only lib
I mean, LXDE is a desktop environment. Does it work on GNOME, which is also a desktop environment?cleverca22 wrote:yep, i was testing it on firefox and gedit, it should work on ANY x program, but i noticed it doesnt work on xterm
For xterm you will need Ctrl+LeftClick "Allow SendEvents", or the corresponding resource.cleverca22 wrote:yep, i was testing it on firefox and gedit, it should work on ANY x program, but i noticed it doesnt work on xterm
Code: Select all
Mario-Fan@raspberrypi ~/Local Netplay $ python setup.py build
running build
running build_ext
building 'xorg' extension
creating build
creating build/temp.linux-armv6l-2.7
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c xorg.c -o build/temp.linux-armv6l-2.7/xorg.o
xorg.c:2:22: fatal error: X11/Xlib.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
Mario-Fan@raspberrypi ~/Local Netplay $