Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Moving the mousepointer in Perl/Tk

by Jouke (Curate)
on Feb 07, 2001 at 14:06 UTC ( [id://56924]=perlquestion: print w/replies, xml ) Need Help??

Jouke has asked for the wisdom of the Perl Monks concerning the following question:

I read in the Tk documentation that there is an event called 'Motion', but I can't find any documentation on how to use eventGenerate to move the mousepointer.
Is it at all possible to let a script move the mousepointer? What I actually want to do is to prevent that the mousepointer leaves the current window. Any ideas?

Jouke Visser, Perl 'Adept'

Replies are listed 'Best First'.
Re: Moving the mousepointer in Perl/Tk
by kschwab (Vicar) on Feb 07, 2001 at 18:46 UTC
    Moving the mouse pointer on behalf of the user is commonly called "warping the pointer".

    Have a look at the -warp portion of eventGenerate. Here's a short example that warps the pointer from a button down to a label when you click the button.

    #!/usr/bin/perl use Tk; $m=Tk::MainWindow->new(); # create a button with a callback to do_warp $b=$m->Button(-text => "Press Me", -command => \&do_warp); # some filler labels so that the pointer warp is obvious $filler1=$m->Label(-text => " "); $filler2=$m->Label(-text => " "); # this label is where the mouse pointer ends up $l=$m->Label(-text => "Mouse ends up here"); $b->pack(); $filler1->pack(); $filler2->pack(); $l->pack(); MainLoop; sub do_warp { $l->eventGenerate('<ButtonPress>',-warp => 1); }
    You could also play with the -x/-y options to place the pointer more specifically. Good luck.

    Update: I should probably mention that moving the pointer around is generally considered "bad juju", and not the best UI design.

      Thanx a zillion already. I know it's bad use in UI design, but in my case it's for a speech-application for disabled, non-speaking people. They have only limited possibilities to operate a mouse, and with a special device and special buttons they still can operate it. What I want to prevent is that accidentally the application loses focus, because they cannot correct that.

      But can I also 'warp' the mousepointer on a mouse-motion event? I tried that, and could not get it to work.

      Jouke Visser, Perl 'Adept'
        Here's an example that keeps the pointer locked inside a label widget. Hopefully this is closer to what you were asking for. You'll have to fill in getting the x/y coordinates for the event from Tk::bind and warping the pointer to a sensible spot

        You may also want to have a look at Tk::grab. You can maintain the focus for keyboard traversal without confining the pointer, if this is useful.

        Familiarize yourself with keyboard control of focus before you run this :)

        #!/usr/bin/perl use Tk; use strict; my $m=Tk::MainWindow->new(); # create a big label so that the window is large # enough to demonstrate this my $label=$m->Label(-bd => 5, -relief => "ridge", -text => "Label Text Here\n" x 10)->pack(); # # bind Leave events from the label to # generate a useless virtual # event. # (We don't really need to fire an event # , we just need the warp option) # Without the -x/-y options, it warps # the pointer to the nw corner of the widget # $label->bind('<Leave>', sub { my($w)=shift; $w->eventGenerate('<<Not Used>>', -warp => 1); } ); MainLoop;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://56924]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-16 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found