Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Set global cursor postion using uinput

Fri Jul 24, 2015 7:23 pm

Hello, I use uinput on Python to emulate mouse and keyboard. When I emulate mouse I can move the cursor to the number of pixels, and how to set global position of cursor?
I use:

Code: Select all

for i in range(2):
        device.emit(uinput.REL_X, 5)
        device.emit(uinput.REL_Y, 5)

User avatar
jojopi
Posts: 3268
Joined: Tue Oct 11, 2011 8:38 pm

Re: Set global cursor postion using uinput

Fri Jul 24, 2015 7:52 pm

A mouse has no absolute position. It reports relative motion only.

If you mean the position of the mouse cursor on the GUI desktop, you should be using xlib functions rather than uinput. However, note that "man XWarpPointer" says "There is seldom any reason for calling this function. The pointer should normally be left to the user."

If your ultimate goal is to automate some manual task, rather than merely annoy the user, there is usually a better way to do it than by faking human input. Remember that almost all the programs in Raspbian are Open Source, so if necessary you can even modify them.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Set global cursor postion using uinput

Fri Jul 24, 2015 9:31 pm

Okey, and the last question how to write special keys (&$%№"#) and cyrillic symbols using uinput?

User avatar
jojopi
Posts: 3268
Joined: Tue Oct 11, 2011 8:38 pm

Re: Set global cursor postion using uinput

Fri Jul 24, 2015 10:37 pm

Keyboards do not send symbols to the computer, they report the pressing and releasing of keys using arbitrary codes based largely on the physical positions.

uinput gives the physical keys names like KEY_Q according to the main character they would type in a US layout. If your Raspbian is set for a Russian layout then KEY_Q may actually type Й.

To get shifted symbols like № you have to simulate pressing KEY_3 while simulating holding KEY_LEFTSHIFT or KEY_RIGHTSHIFT. For some symbols you may need to hold KEY_RIGHTALT.

Again, uinput is very low level and leaves the handling of layouts up to you. It also makes it very difficult to direct the simulated events to a particular program. If it is an X11 client you are trying to control, XSendEvent from xlib may be a better option. I have no experience using that from Python, though.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Set global cursor postion using uinput

Sat Jul 25, 2015 10:21 pm

Okey, I unsderstand you. I try to change language on raspberry pi use uinput, in normal mode (from real-keyboard) I use Shift+Alt. And I try it with uinput, but it doesn't work. What could be the problem?

Code: Select all

device.emit_combo([uinput.KEY_LEFTALT, uinput.KEY_LEFTSHIFT])

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Set global cursor postion using uinput

Sun Jul 26, 2015 3:27 pm

May be problem because I use VNC?

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Set global cursor postion using uinput

Sun Jul 26, 2015 5:42 pm

Yes, problem was because I use VNC, when I use keyboard on Raspberry pi directly all works right.

Return to “Python”