Missing mouse pointer, Raspberry Pi 3, Python 3.4.2 Kivy v1.10.1.dev0

1,782 views
Skip to first unread message

mjkuwp94

unread,
May 11, 2017, 9:42:02 PM5/11/17
to Kivy users support
Hello,

How can I get the mouse pointer to show.  I am able to run most of the examples that came with the build but there are two problems.

1. The mouse cursor does not show
2. The applications take the entire desktop, they do not run in  a Window.  Maybe I can live with that... but I cannot function without a mouse.

Searching around I find references to the mouse pointer problem going back years but I didn't expect it to still exist in this new release. : O

I did try the touchring entry in config.ini and that made one change but does not solve the problem.  It does cause a ring to display on the screen AFTER I press the mouse button and it stays only while I hold the button.

Anyone have the solution for this?


PS:I followed instructions here to do the installation;

mjkuwp94

unread,
May 13, 2017, 7:53:24 PM5/13/17
to Kivy users support
This is really stretching the limit of my skills... but I think I found a portion of the problem.  On Raspberry Pi, one way or maybe the only way to get a mousepointer is via the touchring.py module.  I compared the one in KivyPie with the new one from my 1.10 installation and noticed the mouse pointer code is not in the 1.10 release

Specifically, the method "_mouse_move" is not there.  I think one solution or hack could be to copy the touchring.py from the other versions of Kivy that had this addition.

However, I don't know if this is a proper way around this issue.

Kivy team?  Sorry, but for this project to be labelled as "cross-platform" I think the mouse pointer has to work.  Does anyone have other ideas?

thanks,

Mark

mjkuwp94

unread,
May 16, 2017, 2:45:07 PM5/16/17
to Kivy users support
Final answer : )

There is a module called "cursor.py" that will do this job.  The instructions on how to edit the config.ini file reside inside the file.

on my Windows machine cursor.py is here:
C:\python34\Lib\site-packages\kivy\modules

I added some code to keep the cursor within the bounds of the screen on m Raspberry Pi's HDMI monitor otherwise it is very easy to lose the mouse cursor.

so simple, yet sunk 1.5 days into this


On Thursday, May 11, 2017 at 8:42:02 PM UTC-5, mjkuwp94 wrote:

fzagorsk

unread,
Jun 25, 2017, 4:21:03 PM6/25/17
to Kivy users support
Hello, 

What do you mean by 'it is very easy to lose the mouse cursor' ?

I had the same problem with my PI (running raspbian with pixel not kivy pie), no cursor, I was very surprised for a mutli-platform framework but the issue is fine.

I think what they have done with cursor.py is the implementation of the solution for the problem 'Touchring cursor #1807': make hidinput provider dispatch mouse pos to window and then make touchring able to show mouse cursor (needed for rpi). But instead of modifying touchring there is a nice module cursor.py.
Then adding the following 2 lines in the config.ini works fine
[modules]
cursor = 

I'am running with Kivy v1.10.1.dev0, I don't know when this cursor.py was created. I think a few lines in the documentation for installing on Raspberry will be good.

Was an interesting problem to investigate any way.

mjkuwp94

unread,
Jun 27, 2017, 10:44:30 AM6/27/17
to Kivy users support
You can lose the mouser cursor if you bump the mouse accidentally while not looking and the cursor goes off the screen. There does not seem to be a limit and it will just go go go....  With the cursor off the screen you don't know if it is left, right above or below.  You can pan your mouse randomly but you may be getting more lost.  To get around this one needs code to make the cursor stick to the edge of the screen on whichever edge you are out of range.  This lets the person know which direction to move the mouse to get the cursor back onto the screen.

trija9

unread,
Jun 29, 2017, 3:31:49 AM6/29/17
to Kivy users support
Hi mjkuwp94
How did you manage to adjust the cursor position?

It seems that the on_cursor_leave event does not work, so i use the on_motion event.
But when adjust the motionevent.pos, or Window.mouse_pos or Window.cursor.pos. I shortly see the cursor on the new position but the old postion remains...

    def mouse_clip(self, x, etype, motionevent):
        x = motionevent.pos[0]
        y = motionevent.pos[1]

        if x < 10:
            x = 100
        if x > (Window.width-10):
            x = Window.width-100
        if y > (Window.height-10):
            y = Window.height-100
        if y < 10:
            y =  100
            
        if hasattr(Window, '_cursor'):
            c = Window._cursor
        
        # all changes lost after leaving call
        Window.mouse_pos = (x,y) 
        c.pos = (x,y) 
        motionevent.pos = (x,y)
 

psp...@gmail.com

unread,
Jul 11, 2017, 12:00:50 PM7/11/17
to Kivy users support
Hey, thanks for the advice. Worked for me!

For others, who might like me be bloody beginners on Linux/Pi/Python, and can't find the cursor.py-file:
To simply add a cursor in your kivy-apps, add this to the end of the config.ini in "~./.kivy" (below the [modules] thingy):

cursor=1

There's obviously many different options you can additionally change, like the cursortexture and whatnot. But for me it was sufficient to finally see any cursor at all, so i didn't really bother about those other options.

mjkuwp94

unread,
Jul 18, 2019, 7:30:26 AM7/18/19
to Kivy users support
hi all,

just an update on this topic.  The mouse pointer/cursor requires some modifications when running Kivy on Raspberry pi without the desktop window manager.  I have not noticed any differences in the procedure over the last years with the most recent working one being Kivy stable-1.10.1 on Raspbian Buster with Python 3.7

part of the solution is this:

for each user, needs modifications in /home/[user]/.kivy/config.ini

specifically check these sections:


[input]

mouse = mouse

mtdev_%(name)s = probesysfs,provider=mtdev

%(name)s = probesysfs,provider=hidinput


[modules]

cursor = texture = cursor.png,size=32x32



And the second part

is to edit this file (Using any required adjustments to your specific installed directory):

/usr/local/lib/python3.7/dist-packages/kivy/modules/cursor.py


to constrain the mouse cursor when it would otherwise leave the screen.  This is the code listing from my setup (below) but I doubt this exact code will work for all since it is specific to a certain screen resolution.  You can try comparing this listing to the code in your cursor.py file.  The primary thing done here is to add the function _mouse_move_constrain and bind this to mouse_pos after the first call to the original _mouse_move function.  There is some math that is dependent on the screen resolution and you can experiment with changes to these values.  Try the corners specifically to make sure the cursor does not run off the screen when both the X and Y are out of bounds.


from os.path import join

from functools import partial

__all__ = ('start', 'stop')


from kivy.core.image import Image

from kivy.graphics import Color, Rectangle

from kivy import kivy_data_dir

from kivy.compat import string_types



def _mouse_move_constrain(win, pos):


    if pos[0] < 0:

        _x = 0

    elif pos[0] > 1910:

        _x = 1910

    else:

        _x = pos[0]


    if pos[1] > 1080:

        _y = 1048

    elif pos[1] < 12:

        _y = -20

    else:

        _y = pos[1]-32


    win._cursor.pos = _x, _y



def _mouse_move(texture, size, offset, win, pos, *args):

    global cursor_texture

    global cursor_size

    global cursor_offset

    if hasattr(win, '_cursor'):

     c = win._cursor

    else:

        with win.canvas.after:

            Color(1, 1, 1, 1, mode='rgba')

            win._cursor = c = Rectangle(texture=texture, size=size)

            win.unbind(mouse_pos=_mouse_move)

            win.bind(

                mouse_pos=partial(

                    _mouse_move_constrain))


    #  c.pos = pos[0] + offset[0], pos[1] - size[1] + offset[1]

    c.pos = pos[0], pos[1] - 32



def start(win, ctx):

    cursor_texture = Image(

        ctx.config.get('texture', join(kivy_data_dir, 'images', 'cursor.png'))

    ).texture

    cursor_size = ctx.config.get('size')

    if isinstance(cursor_size, string_types):

        cursor_size = [int(x) for x in cursor_size.split('x')]

    elif not cursor_size:

        cursor_size = cursor_texture.size


    cursor_offset = ctx.config.get('offset', (0, 0))

    if isinstance(cursor_offset, string_types):

        cursor_offset = [int(x) for x in cursor_offset.split('x')]


    win.bind(

        mouse_pos=partial(

            _mouse_move, cursor_texture, cursor_size, cursor_offset))


def stop(win, ctx):

    win.unbind(mouse_pos=_mouse_move_constrain)




hope it helps someone!
Mark


On Thursday, May 11, 2017 at 8:42:02 PM UTC-5, mjkuwp94 wrote:

Wolfmanjm

unread,
Jul 18, 2019, 1:16:32 PM7/18/19
to Kivy users support
FWIW the mouse boundaries issue was fixed in the latest version of Kivy 1.11.x in the hidinput module so the second part of this is not needed in newer version of kivy.
Reply all
Reply to author
Forward
0 new messages