Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Deactivation of mouse motion binding on canvas rectangle fails randomly

已查看 71 次
跳至第一个未读帖子

Alexandru

未读,
2018年10月17日 04:02:332018/10/17
收件人
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

未读,
2018年10月17日 04:42:262018/10/17
收件人
* 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

未读,
2018年10月17日 07:11:222018/10/17
收件人
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

未读,
2018年10月19日 06:34:442018/10/19
收件人
Thanks Ralf, I will try this.

Alexandru

未读,
2018年10月19日 06:36:572018/10/19
收件人
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

未读,
2018年10月20日 09:15:412018/10/20
收件人
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

未读,
2018年10月20日 09:17:412018/10/20
收件人
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

未读,
2018年10月20日 11:43:462018/10/20
收件人
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

未读,
2018年10月21日 04:44:242018/10/21
收件人
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

未读,
2018年10月22日 05:10:542018/10/22
收件人
* 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

未读,
2018年10月23日 07:21:582018/10/23
收件人
Okay, I will try to provide a test code.
0 个新帖子