tuomasjjrasanen / python-uinput Goto Github PK
View Code? Open in Web Editor NEWPythonic API to Linux uinput module
Home Page: http://tjjr.fi/sw/python-uinput/
License: GNU General Public License v3.0
Pythonic API to Linux uinput module
Home Page: http://tjjr.fi/sw/python-uinput/
License: GNU General Public License v3.0
I can get keyboard and mouse working, and have a xbox360 as joystick, it can control games like torcs and flightgear, but cannot get it working here.
Also, seems no js0 js1.. specified,
I tried SCROLLUP and SCROLLDOWN key without success. I presume there are extra parameters necessary to make this work, but I do not see a special case to provide these.
I see no acivity on tickets and a few pending pull request, I like to know the project status or if there is an alternate repo from someone that is continuing this work.
Hi, this is my code: https://pastebin.com/GGEKdHSR
When this code runs, the /devices/virtual/input/inputXXX increases with +1 increment each second.
https://puu.sh/y6BFb/f27e8004a2.png (screenshot)
subj
I would like to make the mouse pointer/cursor move independently and directly to the given x and y positions without the need to use the relative attributes (REL_X, REL_Y) seen that these two ones make the mouse movement using as reference the actual x and y positions. I wrote this script below to try make the mouse pointer goes directly but nothing happens.
import uinput
from time import sleep
def createDevice():
device = uinput.Device([
uinput.ABS_X,
uinput.ABS_Y,
uinput.BTN_LEFT,
uinput.BTN_RIGHT,
])
return device
def emitActions(device):
sleep(1)
device.emit(uinput.ABS_X, 26, syn=False)
sleep(1)
device.emit(uinput.ABS_Y, 665, syn=False)
sleep(1)
device.syn()
sleep(1)
device.emit(uinput.BTN_LEFT, 1)
sleep(1)
device.emit(uinput.BTN_LEFT, 0)
def main():
dev = createDevice()
emitActions(dev)
if __name__ == "__main__":
main()
When I debugged this code I didn't get errors, but I would like to know what's missing there in this code to make that the mouse pointer goes to the desired x and y positions?
Doesn't work with console Advance Mame on Raspberry Pi. I'm not sure why but key press/release event may be happening too quickly and is being missed by console Advance Mame (No X or SDL) entirely. Works fines with other Raspberry Pu console programs.
Is there any? Or is potential user supposed to understand the lib's possibilities, options, advanced usage etc using only comments on your website, those 3 short examples and couple hours getting to know it?
I find some key defines like KEY_KP*
eg KEY_KP1
, what do these keys mean?
what's the different between KEY_KP1
and KEY_1
My code:
import uinput
def main():
events = (
uinput.BTN_JOYSTICK,
uinput.ABS_X + (0, 255, 0, 0),
uinput.ABS_Y + (0, 255, 0, 0),
)
with uinput.Device(events) as device:
while True:
# syn=False to emit an "atomic" (5, 5) event.
device.emit(uinput.ABS_X, 5, syn=False)
device.emit(uinput.ABS_Y, 5)
device.emit_click(uinput.BTN_JOYSTICK)
if __name__ == "__main__":
main()
So I'm trying to simulate a stylus like this:
vd = uinput.Device([
uinput.ABS_X,
uinput.ABS_Y,
uinput.ABS_PRESSURE,
uinput.ABS_DISTANCE,
uinput.ABS_TILT_X,
uinput.ABS_TILT_Y,
])
and xinput doesn't display it. However if I replace these capabilities with a mouse's capabilities the device gets displayed.
Seems like there are only certain configurations possible and I haven't figured out yet which they are and how to add more.
@tuomasjjrasanen Could you please let us know if you are still maintaining this project?
There are a few good pull requests that are unmerged, and uncoordinated forks have been popping up.
I'm happy to help with project maintenance on this repo, or doing a more coordinated forking with the rest of the community.
I can't find the keycode for a colon
sysconfig.get_config_var("SO") is used in init.py:
_libsuinput_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "_libsuinput" + sysconfig.get_config_var("SO")))
but returns None in python 3.11:
python --version
Python 3.11.3
python
Python 3.11.3 (main, Apr 5 2023, 15:52:25) [GCC 12.2.1 20230201] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> print(sysconfig.get_config_var("SO"))
None
leading to errors like:
Traceback (most recent call last):
File "/usr/lib/keyboard-center/main.py", line 8, in <module>
from mainUi import MainWindow
File "/usr/lib/keyboard-center/mainUi.py", line 23, in <module>
from devices.keyboard import SUPPORTED_DEVICES, KeyboardInterface
File "/usr/lib/keyboard-center/devices/keyboard.py", line 1, in <module>
import uinput
File "/usr/lib/python3.11/site-packages/uinput/__init__.py", line 86, in <module>
_libsuinput_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "_libsuinput" + sysconfig.get_config_var("SO")))
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: can only concatenate str (not "NoneType") to str
To fix change to
sysconfig.get_config_var("EXT_SUFFIX")
Getting this error when i run the example code.
Downstream bug report: https://bugs.debian.org/1071852
arm-linux-gnueabihf-gcc -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -Werror=implicit-function-declaration -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -g -O2 -Werror=implicit-function-declaration -ffile-prefix-map=/<<PKGBUILDDIR>>=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.11 -c libsuinput/src/suinput.c -o build/temp.linux-armv7l-cpython-311/libsuinput/src/suinput.o
libsuinput/src/suinput.c: In function ‘suinput_emit’:
libsuinput/src/suinput.c:48:28: error: ‘struct input_event’ has no member named ‘time’
48 | gettimeofday(&event.time, 0);
| ^
error: command '/usr/bin/arm-linux-gnueabihf-gcc' failed with exit code 1
A similar issue can be found at FreeSpacenav/spacenavd#104 . A possible solution could be https://github.com/libsdl-org/SDL/pull/7884/files .
Running python-uinput on my pi 2B+ here, but I cannot get it to actually press keys.
uinput module is loaded, there are no errors when I run my python script, but the key isn't pressed.
Test code:
http://pastebin.com/bD3vRH6B
I'm checking for inputs using xev
.
Hello guys,
I've tried to install python-uinput manually and automatically via pip, but neither of these options worked;
When execute sudo pip install python-uinput
I receive the following error:
$ sudo pip install python-uinput
Collecting python-uinput
Using cached https://files.pythonhosted.org/packages/54/b7/be7d0e8bbbbd440fef31242974d92d4edd21eb95ed96078b18cf207c7ccb/python-uinput-0.11.2.tar.gz
Installing collected packages: python-uinput
Running setup.py install for python-uinput ... error
Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-e7gyxby9/python-uinput/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-zx1wj8ct/install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.7
creating build/lib.linux-x86_64-3.7/uinput
copying src/ev.py -> build/lib.linux-x86_64-3.7/uinput
copying src/__init__.py -> build/lib.linux-x86_64-3.7/uinput
running build_ext
building '_libsuinput' extension
creating build/temp.linux-x86_64-3.7
creating build/temp.linux-x86_64-3.7/libsuinput
creating build/temp.linux-x86_64-3.7/libsuinput/src
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DOPENSSL_LOAD_CONF -fwrapv -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -fPIC -I/usr/include/python3.7m -c libsuinput/src/suinput.c -o build/temp.linux-x86_64-3.7/libsuinput/src/suinput.o
gcc -pthread -shared build/temp.linux-x86_64-3.7/libsuinput/src/suinput.o -L/usr/lib64 -l:libudev.so.1 -lpython3.7m -o build/lib.linux-x86_64-3.7/_libsuinput.cpython-37m-x86_64-linux-gnu.so
/usr/lib64/gcc/x86_64-suse-linux/8/../../../../x86_64-suse-linux/bin/ld: cannot find -lpython3.7m
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-e7gyxby9/python-uinput/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-zx1wj8ct/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-e7gyxby9/python-uinput/
When trying to compile it manually, this error is shown with Python3:
$ python setup.py build
running build
running build_py
running build_ext
building '_libsuinput' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DOPENSSL_LOAD_CONF -fwrapv -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -fPIC -I/usr/include/python3.7m -c libsuinput/src/suinput.c -o build/temp.linux-x86_64-3.7/libsuinput/src/suinput.o
gcc -pthread -shared build/temp.linux-x86_64-3.7/libsuinput/src/suinput.o -L/usr/lib64 -l:libudev.so.1 -lpython3.7m -o build/lib.linux-x86_64-3.7/_libsuinput.cpython-37m-x86_64-linux-gnu.so
/usr/lib64/gcc/x86_64-suse-linux/8/../../../../x86_64-suse-linux/bin/ld: cannot find -lpython3.7m
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
With python 2:
$ python setup.py build
running build
running build_py
running build_ext
building '_libsuinput' extension
gcc -pthread -fno-strict-aliasing -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DNDEBUG -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DOPENSSL_LOAD_CONF -fwrapv -fPIC -I/usr/include/python2.7 -c libsuinput/src/suinput.c -o build/temp.linux-x86_64-2.7/libsuinput/src/suinput.o
gcc -pthread -shared build/temp.linux-x86_64-2.7/libsuinput/src/suinput.o -L/usr/lib64 -l:libudev.so.1 -lpython2.7 -o build/lib.linux-x86_64-2.7/_libsuinput.so
/usr/lib64/gcc/x86_64-suse-linux/8/../../../../x86_64-suse-linux/bin/ld: cannot find -lpython2.7
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
Does anyone know how to fix that?
I'm running kernel 5.0.3.
Thanks in advance!
Cheers!
Hello,
Sorry if this is "user error" I'm still learning python and trying to learn by doing a project.
I've looked at the joystick and example and then it failed to provide me with /dev/input/js4 or other device in the KDE joystick manager.
so I tried to use the example for the keyboard
import uinput
device = uinput.Device([
uinput.KEY_E,
uinput.KEY_H,
uinput.KEY_L,
uinput.KEY_O,
])
device.emit_click(uinput.KEY_H)
device.emit_click(uinput.KEY_E)
device.emit_click(uinput.KEY_L)
device.emit_click(uinput.KEY_L)
device.emit_click(uinput.KEY_O)
and I was expecting this to output Hello on my terminal
but I'm getting nothing, no errors or output.
I don't know how to get more debugging information out of python at the moment.
@tuomasjjrasanen Would you like to perform the uploads of the new releases, or would you rather I do it?
If you'd like you can add me on https://pypi.org/manage/project/python-uinput/collaboration/
Another option is coordinating on importing back the changes from the python-uinput2 repo
The distutils module is removed in Python 3.12. Please consider migrating to other tools, such as setuptools.
Hi.
I love how this work but i'd love it to have more button support? Where is the limitation on this? I mean there are buttons e.g. BTN_TR, BTN_A, BTN_THUMB?
Reason i ask is because i have made my own "controller" and want to use uinput as a driver for it, and i cant get it to act like my xbox usb controller? Mapping buttons like top left trigger e.t.c?
Are uinput the limitation?
As stated above,
BTN_SOUTH = (0x01, 0x130)
BTN_A = (0x01, BTN_SOUTH)
BTN_EAST = (0x01, 0x131)
BTN_B = (0x01, BTN_EAST)
BTN_C = (0x01, 0x132)
BTN_NORTH = (0x01, 0x133)
BTN_X = (0x01, BTN_NORTH)
BTN_WEST = (0x01, 0x134)
BTN_Y = (0x01, BTN_WEST)
BTN_Z = (0x01, 0x135)
Your code under init on parsing of the inputs:
for ev_spec in self.__events:
ev_type, ev_code = ev_spec[:2]
BTN_A will be (0x01, (0x01, 0x130)) which crashes the system on that line. Dont know how that is fixed, you probably do +1
This script, based on the provided keyboard example:
import time
import uinput
def main():
events = (uinput.KEY_A, uinput.KEY_Z,)
with uinput.Device(events) as device:
time.sleep(1)
device.emit_click(uinput.KEY_A)
device.emit_click(uinput.KEY_Z)
if __name__ == "__main__":
main()
... produce qw
instead of az
.
I use an Azerty keyboard.
When running the command python setup.py build
, compiler returns an error, stating "Cannot open shared object file libudev.so".
The problem appears to be the caused by the replacement of libudev.so
with libudev.so.0
(older version with deprecated interface which may not exist in current libudev
builds) and libudev.so.1
(new version being supported) as part of its merge into systemd
.
This error was hotfixed by replacing "libudev.so"
in setup.py
with "libudev.so.1"
, but requires further testing to determine full cross-compatibility.
Hi!
I've been able to install uinput by following comments on this issue [https://github.com//issues/3] and I wanted to try the library.
After a copy&paste on a new file, I've started (or at least I think so) uinput kernel module (I've tried with modprobe uinput, modprobe -i uinput and sudo modprobe -i uinput.
When I run the file with the keyboard example a error appears:
~/Keyboard emulator/python script/$ python keyboard-emulator.py
Traceback (most recent call last):
File "keyboard-emulator.py", line 4, in
with uinput.Device([uinput.KEY_E, uinput.KEY_H,uinput.KEY_L, uinput.KEY_O]) as device:
File "/usr/local/lib/python2.7/dist-packages/uinput/init.py", line 175, in init
self.uinput_fd = _libsuinput.suinput_open()
File "/usr/local/lib/python2.7/dist-packages/uinput/__init.py", line 70, in _open_error_handler
raise OSError(code, msg)
OSError: [Errno 13] Failed to open the uinput device: Permission denied
If I use sudo, there is no error, but there is also no visible input. I think there is maybe some issue with terminal, so I've added a 5 seconds delay and moved to a text editor but still nothing.
I've tried also the mouse example, but the same errors appears.
What should I do?
Thanks!
see https://github.com/tuomasjjrasanen/python-uinput/blob/master/setup.py#L10
Path here should now be /usr/include/linux/input-event-codes.h
(since Oct 2015- https://github.com/torvalds/linux/commits/master/include/uapi/linux/input-event-codes.h)
I don't seem to be able to generate the " character. The keysym "quotedbl" used by TK doesn't exist. I checked input.h but I can't find it there either.
What am I missing?
Same code works in an interactive shell, but not sudo python keypress.py start
no errors, just not expected beahviour
import uinput
import sys
arg = sys.argv[1]
if arg == 'start':
print('ENTER press')
device = uinput.Device([uinput.KEY_ENTER]); device.emit(uinput.KEY_ENTER, 1)
elif arg == 'select':
print('SELECT press')
elif arg == 'exit':
print('SHIFT/ESCAPE press')
device = uinput.Device([uinput.KEY_LEFTSHIFT, uinput.KEY_ESC]); device.emit(uinput.KEY_LEFTSHIFT, 1); device.emit(uinput.KEY_ESC, 1)
I'm not exactly sure what's going wrong, but python-uinput seems to be not outputting mouse commands on my machine (Ubuntu 18.10). Keyboard commands all work,.
I've downloaded master and run python setup.py build/install, so I'll be debugging it on my end, but any suggestions as to causes, solutions, or things to investigate would be greatly appreciated.
MVP:
>>> import uinput
>>> events = (uinput.BTN_LEFT,)
>>> device = uinput.Device(events)
>>> device.emit_click(uinput.BTN_LEFT)
(expect left click, get nothing)
via /proc/bus/input/devices:
II: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="python-uinput"
P: Phys=
S: Sysfs=/devices/virtual/input/input54
U: Uniq=
H: Handlers=event259
B: PROP=0
B: EV=3
B: KEY=10000 0 0 0 0
I'm sure I must be doing something wrong. I'm attempting to run the examples and I get this error:
Traceback (most recent call last):
File "mouse.py", line 3, in
import uinput
File "/usr/lib/python2.7/site-packages/uinput/init.py", line 42, in
_ABS_CNT = ABS_MAX[1] + 1
NameError: name 'ABS_MAX' is not defined
I'm on Arch and I've tried python3 and 2
Is there any way to emit extended characters, like ç, ã, ø, å ?
It doesn't seem feasible by looking at the code in my limited understanding.
By the way, nice work! Quite useful module for my application.
Thanks!
Hello!
I love this library because it's very simple and easy!
But I've been searching, but haven't found any list of all the possible keys that you can emulate with it... Like is scroll possible and something like KEY_ENTER?
Thanks!
I'd like to announce the creation of python-uinput2
This is a continuation of the great work done by Tuomas and others, with the following goals:
We have decided to fork the original project due to the accumulation of issues, contributions and PRs.
In Tuomas' words: tempus edax rerum
Most of the outstanding issues / PRs for python-uinput (1) have already been integrated. We welcome bug reports, contributions and new maintainers.
Existing code will keep working with no changes (including import statements).
Note that the package name changed to python-uinput2
(available through pip / pypi).
I'd like to express my great gratitude for all the work that Tuomas and other contributors put into the software. The fact that it simply keeps working after 8 years with no updates is a testament to how well written it is.
Hi, It's me again! :D
I have mapped the buttons ABXY on a jstest it shows:
Axes: 0:-32767 1:-32767 Buttons: 0:off 1:off 2:off 3:off 4:off 5:off 6:off 7:off 8:off
Where:
0 = BTN_JOYSTICK
1 = BTN_A
2 = BTN_B
3 = BTN_Y
4 = BTN_X
But on my xbox controller its correct:
0 = BTN_A
1 = BTN_B
2 = BTN_X
3 = BTN_Y
Would love it to be mapped in order of "events" or something so you can customize it? :)
I've been trying to run the example keyboard script on a Raspberry Pi 3B+ running Raspbian Stretch (both as root and as a normal user, with /dev/uinput set temporarily to +0666 for testing), but I don't get the scripted keyboard events. A quick check of /var/log/syslog reveals the following occurs when the script is run:
systemd-udevd[987]: Process '/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev' failed with exit code 1.
Any thoughts? Sorry for the trouble, and many thanks.
No matter where I include the lib, it always has the same error...
Traceback (most recent call last):
File "joystick.py", line 1, in
import uinput
File "/usr/lib/python2.7/site-packages/uinput/init.py", line 42, in
_ABS_CNT = ABS_MAX[1] + 1
NameError: name 'ABS_MAX' is not defined
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.