[plt-scheme] Mouse and Canvas Object

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Apr 13 08:54:48 EDT 2003

At Fri, 11 Apr 2003 07:41:03 +0200, "The Computer Man" wrote:
> I would like to create a function which awaits that the utilisitator is
> clicked with the left button of his mouse in the canvas, thereafter that
> recovers co-ordinate X then the co-ordinate Y then that rounds these
> co-ordinates there and finally if they well the left button of then in a
> hurry one calls a function.
> This is the function was writing but not running, i'm define a mouse event
> 'left-down
> (define mouse (make-object mouse-event% 'left-down))
> (define (wait-click j)
>   (let* ((posnx (send mouse get-x))
>          (posny (send mouse get-y)))
>     (define x (- (+ posnx (- 40 (modulo posnx 40))) 40))
>     (define y (- (+ posny (- 40 (modulo posny 40))) 40))
>     (cond ((send mouse button-down? 'left) draw(x y j)))))

This is not how event-based GUI programming works. The event model is
"don't call us --- we'll call you".

At Sun, 13 Apr 2003 09:35:42 +0200, "The Computer Man" wrote:
> I'm add your define for the event, but i don't understand hom i can use it,
> for example i'm click with the left button, how can call the function
> (on-event e) ?

`on-event' in canvas% is one of the places the the event-generator
calls, and that's where you do the work that needs to be triggered by a
click.

Here's a small example:

 (define my-canvas%
  (class canvas%
    (define/override (on-event evt)
      (cond
        [(send evt button-down?)
         (printf "Click at (~a,~a) in the canvas~n"
                 (send evt get-x) (send evt get-y))]))
    (super-instantiate ())))

 (define f (instantiate frame% ("Demo")
             [width 200] [height 200]))
 (instantiate my-canvas% (f))
 (send f show #t)

There's no way to globally wait until the mouse is clicked, or even to
get the current location of the mouse pointer. The only thing you can
do is wait for an `on-event' method to be called when the mouse is
moved or button is clicked.

Matthew



Posted on the users mailing list.