1

当我将它作为脚本运行时,Python-uinput 无法移动光标

python main.py

但是当我使用 Python 交互式逐步运行时,它工作正常

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()

这是一个不移动光标的命令。

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 $

这是一个与 python 交互的命令,它可以正确移动光标。

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)
>>> 

测试视频:Python-uinput 光标确实移动测试

4

1 回答 1

1

至少在 X11 下,因为您的测试代码完成得如此突然,在代码退出之前,设备的处理程序没有时间创建。
解决方案,仅针对代码退出时的这些测试用例,是添加一个time.sleep()命令。
在实时运行条件下,这将是不需要的。

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()
于 2016-03-25T09:34:19.100 回答