[plt-scheme] trapping key events in text-field%

From: Gregory Cooper (ghcooper at gmail.com)
Date: Sat Nov 29 14:53:04 EST 2008

Hi,

What's the right way to catch key events in a text-field%?  I thought
I should override its on-subwindow-char, but that seems to catch each
event twice.  E.g., if I run the following program and type "a" into
the text field, it displays:

1: a
2: a
3: release
4: release

If I remove the (super ...) call, I get:

1: a
2: release

but then the "a" doesn't appear in the text field.

Thanks,
Greg

-------

(require mred/mred)
(require scheme/class)

(define frame (new frame%
                   [label "Example"]
                   [min-width 640]))

(define input (new (class text-field%
                     (super-new)
                     (define count 0)
                     (define/override (on-subwindow-char t e)
                       (set! count (add1 count))
                       (printf "~a: ~a~n" count (send e get-key-code))
                       (super on-subwindow-char t e)))
                   [parent frame]
                   [label ""]))

(send frame show #t)


Posted on the users mailing list.