1

Python-uinput it can't move cursor when I run it as a script

python main.py

But when I run step by step with Python interactive It works fine

import uinput
import Tkinter as tk

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.destroy()

device = uinput.Device([
         uinput.BTN_LEFT,
         uinput.BTN_RIGHT,
         uinput.REL_X,
         uinput.REL_Y,
         ])
def main():
    device.emit(uinput.REL_X, -1 * screen_width)
    device.emit(uinput.REL_Y, -1 * screen_height)
if __name__ == '__main__':
    main()

This is a command that it does not move a cursor.

spksoft@spksoft ~/code/python/Sample HI $ python main.py
Traceback (most recent call last):
  File "main.py", line 13, in <module>
    uinput.REL_Y,
  File "/usr/local/lib/python2.7/dist-packages/uinput/__init__.py", line 161, in __init__
    self.__uinput_fd = _libsuinput.suinput_open()
  File "/usr/local/lib/python2.7/dist-packages/uinput/__init__.py", line 64, in _error_handler
    raise OSError(code, os.strerror(code))
OSError: [Errno 13] Permission denied
spksoft@spksoft ~/code/python/Sample HI $ sudo python main.py
spksoft@spksoft ~/code/python/Sample HI $

This is a command with python interactive and It can move a cursor correctly.

spksoft@spksoft ~/code/python/Sample HI $ sudo python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import uinput
>>> import Tkinter as tk
>>> root = tk.Tk()
>>> screen_width = root.winfo_screenwidth()
>>> screen_height = root.winfo_screenheight()
>>> root.destroy()
>>> device = uinput.Device([
...         uinput.BTN_LEFT,
...         uinput.BTN_RIGHT,
...         uinput.REL_X,
...         uinput.REL_Y,
...         ])
>>> device.emit(uinput.REL_X, -1 * screen_width)
>>> device.emit(uinput.REL_Y, -1 * screen_height)
>>> 

test video : Python-uinput cursor does move test

  • have you verfied that `screen_width` and `screen_height` have been properly computed? Perhaps your calculation causes the cursor to move zero pixels. – Bryan Oakley Oct 27 '15 at 14:36
  • Yes, I wanna move cursor to zero position (0,0). A code work fine when I copy and paste it on a python interactive but when I run it as a script by using this command "python main.py" it does not show any error but it does not move a cursor. – Sippakron Raksakide Oct 27 '15 at 17:00
  • That doesn't answer my question. Have you verified that `-1 * screen_width` is returning what you think it's returning? – Bryan Oakley Oct 27 '15 at 17:03
  • Sorry for my answer, Yes, I already verify. A screen_width and a screen_height contain my resolution and I multiply them because I wanna move a cursor to zero position. I already change "-1 * screen_width" and "-1 * screen_height" to a constant number such as 1, 2, 3, ... but it does not move a cursor. – Sippakron Raksakide Oct 27 '15 at 17:31
  • For more information, I recorded a test video [link](https://www.youtube.com/watch?v=WmxLUtnCiHI&feature=youtu.be) – Sippakron Raksakide Oct 27 '15 at 18:11

1 Answers1

1

Under X11, at least, because your test code finishes so abruptly, the handler for the device doesn't have time to be created, before the code exits.
The solution, just for these test cases when the code exits, is to add a time.sleep() command.
Under live running conditions, this wouldn't be needed.

import uinput
import Tkinter as tk
import time
root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.destroy()

device = uinput.Device([
         uinput.BTN_LEFT,
         uinput.BTN_RIGHT,
         uinput.REL_X,
         uinput.REL_Y,
         ])
def main():
    time.sleep(1)
    device.emit(uinput.REL_X, -1 * screen_width)
    device.emit(uinput.REL_Y, -1 * screen_height)
if __name__ == '__main__':
    main()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60