[plt-scheme] Re: MrEd - Animated canvas

From: Robby Findler (robby.findler at gmail.com)
Date: Sat Mar 10 07:09:51 EST 2007

Sorry for the delayed response.

In general, keyboard events do not go to a subwindow of a frame unless
that window has the focus. Clicking is one way to set the focus.
Calling the `focus' method is another way to set the focus.

Another problem with the code below is that it is mis-parenthesized.

If you put it into drscheme, select all and hit "tab", you'll see this:

(define my-canvas%
  (class canvas%
    (define/override (on-char evt)
      (case (send evt get-key-code))
      ((left) (printf "left\n"))
      ((right) (printf "right\n"))
      (else (printf "Another key\n")))
    (super-new)))

which should be a clue -- the "((left) ...)" part should be indented a
little bit behind "case", but there is an extra close parenthesis on
the line with "case".

Anyways, once that is fixed, these lines makes the printouts show up for me:

(define my-canvas%
  (class canvas%
    (define/override (on-char evt)
      (case (send evt get-key-code)
        ((left) (printf "left\n"))
        ((right) (printf "right\n"))
        (else (printf "Another key\n"))))
    (super-new)))

(define f (new frame% [label ""] [width 100] [height 100]))
(define c (new my-canvas% [parent f]))
(send c focus)
(send f show #t)

Also note that the "another key" message is not always actually
another key, but might be a key release event.

hth,
Robby

On 3/9/07, Laurent <Ouaibou at gmail.com> wrote:
> Nodoby can help me ? :(
>
> On 7 mar, 15:42, "Laurent" <Ouai... at gmail.com> wrote:
> > With your idea, the flutters disappeared. It is super, thank you:)
> >
> > Now, I seek how to associate events keyboard to an action. But my code
> > doesn't want to work :
> >
> > (define my-canvas%
> >   (class canvas%
> >     (define/override (on-char evt)
> >       (case (send evt get-key-code))
> >         ((left) (printf "left\n"))
> >         ((right) (printf "right\n"))
> >         (else (printf "Another key\n")))
> >     (super-new)))
> >
> > Moreover, it is necessary to click in the canvas in order to events
> > keyboards start.
> >
> > I don't understand why ?
> >
> > Thanks,
> > Laurent
> >
> > _________________________________________________
> >   For list-related administrative tasks:
> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.