Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deactivation of mouse motion binding on canvas rectangle fails randomly

71 views
Skip to first unread message

Alexandru

unread,
Oct 17, 2018, 4:02:33 AM10/17/18
to
On Tcl/Tk 8.6.7 I have a canvas with some rectangles that I can move around with my mouse. I have defined bindings like this:

$graphplane bind point <1> {2DGraphPointSelect %x %y}
$graphplane bind point <B1-Motion> {2DGraphPointDrag %x %y}

I checked the order of bindings execution and it's first <1> then <B1-Motion>

Everything works fine.

Now I want to artificially move the mouse pointer to the middle of the rectangle, everytime I select a rectangle. I do this using Twapi.

At the same time I want to deactivate the Motion binding just before twapi moves the mouse to the center of the rectangle and then activate the binding back. Otherwise I could never move the mouse to the center of the rectangle.

The code inside the <1> binding looks like this:

$graphplane bind point <B1-Motion> {}
::twapi::move_mouse $dx $dy -relative
$graphplane bind point <B1-Motion> {2DGraphPointDrag %x %y}

This code does not work at all, as the Motion binding is still called although I explicitly deactivate the binding.

The I added the busy command:

$graphplane bind point <B1-Motion> {}
tk busy hold $graphplane
update
::twapi::move_mouse $dx $dy -relative
tk busy forget $graphplane
update
$graphplane bind point <B1-Motion> {2DGraphPointDrag %x %y}

This code works most of the time but it fails randomly (the motion bindig is still executed some times)

Any idea what I'm missing here?

Thank you!
Alexandru

Ralf Fassel

unread,
Oct 17, 2018, 4:42:26 AM10/17/18
to
* Alexandru <alexandr...@meshparts.de>
| Now I want to artificially move the mouse pointer to the middle of the
| rectangle, everytime I select a rectangle. I do this using Twapi.
>
| At the same time I want to deactivate the Motion binding just before
| twapi moves the mouse to the center of the rectangle and then activate
| the binding back. Otherwise I could never move the mouse to the center
| of the rectangle.

Add the Motion binding only in the proc which handles the button click,
after the mouse has moved:

Pseudo:

bind 1-press pressproc
bind 1-release releaseproc

proc pressproc
move mouse to desired position
bind 1-motion motion-handler

proc motion-handler
https://core.tcl.tk/tips/doc/trunk/tip/131.md

proc releaseproc
bind 1-motion ""

?
R'

Rich

unread,
Oct 17, 2018, 7:11:22 AM10/17/18
to
Alexandru <alexandr...@meshparts.de> wrote:
> Now I want to artificially move the mouse pointer to the middle of
> the rectangle, everytime I select a rectangle. I do this using
> Twapi.

Have you tried using the 'event generate' subcommand with -warp true to
send a motion event for moving the mouse cursor?

Using Tk's event generate might (or might not) to move the mouse to the
center of the rectangle might work better than using twapi to do the
same.



Now, my 2-cents. Please don't do this. I always find it disconcerning
when some program decides to "jump" the mouse pointer around to another
location on screen without my moving the mouse physically to cause such
a change. I.e., let the user be in control. The mouse position
belongs to the user, and only the user's physical manipulation of the
mouse input device should ever cause the pointer to change its on
screen position.

Alexandru

unread,
Oct 19, 2018, 6:34:44 AM10/19/18
to
Thanks Ralf, I will try this.

Alexandru

unread,
Oct 19, 2018, 6:36:57 AM10/19/18
to
The reactangle is very small (2 or 3 pixels wide). The mouse motion to the center is not realy perceptible. Doing that makes life easier for writing the code behind. In general I agree with your point and never do that.

Alexandru

unread,
Oct 20, 2018, 9:15:41 AM10/20/18
to
I realized, I cannot activate the motion binding when the button is released since I stil want to be able to drag the rectangle. I tried to just activate the motion binding shortly before I move the mouse to the center of the rectangle but the procedure still fails to deactivate the motion binding randomly.

My problen is that I don't get it why?

I will add a global flag variable, that will be asked in the motion callback procedure. If I set the variable to 0, then the motion callback will return immediatly. Let's see what it brings...

Alexandru

unread,
Oct 20, 2018, 9:17:41 AM10/20/18
to
Am Mittwoch, 17. Oktober 2018 13:11:22 UTC+2 schrieb Rich:
> Alexandru <alexandr...@meshparts.de> wrote:
> > Now I want to artificially move the mouse pointer to the middle of
> > the rectangle, everytime I select a rectangle. I do this using
> > Twapi.
>
> Have you tried using the 'event generate' subcommand with -warp true to
> send a motion event for moving the mouse cursor?
>
> Using Tk's event generate might (or might not) to move the mouse to the
> center of the rectangle might work better than using twapi to do the
> same.
>
I tried event generate, but the behavior remains the same. Twapi is easier to use in my case, since I can simply specify the -relative option, and the mouse move relative to the old position. This option would be nice in "event generate" too.

jda...@gmail.com

unread,
Oct 20, 2018, 11:43:46 AM10/20/18
to
How is centering the mouse pointer helpful?
It seems like moving would work fine without that:

package require Tk

destroy .c
pack [canvas .c]
.c create rectangle 10 10 50 50 -fill red -tags movable

.c bind movable <ButtonPress-1> {selRect %W %x %y}

proc selRect {w x y} {
set id [lindex [$w find closest $x $y] 0]
$w addtag moving withtag $id
bind $w <B1-Motion> "move $w $x $y %x %y"
bind $w <ButtonRelease-1> "stop $w $id"
}

proc stop {w id} {
bind $w <B1-Motion> ""
bind $w <ButtonRelease-B1> ""
$w dtag $id moving
}

proc move {w x0 y0 x1 y1} {
$w move moving [expr {$x1-$x0}] [expr {$y1-$y0}]
bind $w <B1-Motion> "move $w $x1 $y1 %x %y"
}

If a reference centered on the moved rectangle is needed, draw crosshairs centered on the rectangle (tagged moving) while the move is occuring.

Dave

Alexandru

unread,
Oct 21, 2018, 4:44:24 AM10/21/18
to
I reformulate my problem:
I don't understand, why the deactivation of the motion binding fails randomly.
If anybody could help in this matter, I would be very thankfull for any advices.

Ralf Fassel

unread,
Oct 22, 2018, 5:10:54 AM10/22/18
to
* Alexandru <alexandr...@meshparts.de>
| I reformulate my problem:
| I don't understand, why the deactivation of the motion binding fails randomly.
| If anybody could help in this matter, I would be very thankfull for any advices.

It would certainly help if you could show your code (stripped down
enough to show the problem). If the problem does not occur in the
stripped down example, it is an indication that the rest of your app
might interfer here...

R'

Alexandru

unread,
Oct 23, 2018, 7:21:58 AM10/23/18
to
Okay, I will try to provide a test code.
0 new messages