[plt-scheme] event binding help
At Sat, 28 Feb 2004 16:30:12 -0800, briand at aracnet.com wrote:
> I was hoping that the eventspace
> functionality would allow me to do something similar, but if it does I
> can't figure it out.
If I understand the problem, then I agree that eventspaces don't help.
> Unfortunately, after perusing the documentation, it's not obvious to
> me how to do this without simply putting a brute force event filter in
> on-subwindow-event and dispatching based on mode. And that seems like
> a very kludgey way to do it.
I don't quite understand this, though. How is that different from what
Tk does (aside from the fact that Tk provides it built-in)? I'm also
not sure why `on-subwindow-event' is the right place to put the filter,
instead of `on-event', etc.
More code below, but it's in the same spirit as Bruce's suggestion, so
I'm not sure I'm on the right track.
Matthew
----------------------------------------
;; Nicer event predicates for mouse/key mixtures
(define (button-down? e)
(and (e . is-a? . mouse-event%)
(send e button-down?)))
(define (button-up? e)
(and (e . is-a? . mouse-event%)
(send e button-up?)))
(define (dragging? e)
(and (e . is-a? . mouse-event%)
(send e dragging?)))
(define (key-press? e)
(and (e . is-a? . key-event%)
(not (eq? (send e get-key-code)
'release))))
(define (key-release? e)
(and (e . is-a? . key-event%)
(eq? (send e get-key-code)
'release)))
(define (event-mixin %)
(class %
(define/override (on-event e)
(do-dispatch e))
(define/override (on-char e)
(do-dispatch e))
(define current-target #f)
(define/private (do-dispatch e)
(if current-target
(begin
(current-target e)
(when (or (button-up? e)
(key-release? e))
(set! current-target #f)))
(begin
(set! current-target (dispatch e))
(when current-target
(do-dispatch e)))))
;; dispatch : mouse-or-key-event -> #f or (mouse-or-key-event -> )
;; Selects an event handler based on an initial event.
;; This event handler keeps control until after a mouse up
;; or key up event
(define/public (dispatch e)
#f)
(super-new)))
;; Example use
(define f (make-object frame% "Hello"))
(define c (make-object
(class (event-mixin canvas%)
(define/override (dispatch e)
(cond
[(button-down? e)
;; This handler gets everything from button down to button up
(lambda (e)
(printf "Button handler got ~e~n" e))]
[(key-press? e)
;; This handler gets everything from key down to key up
(lambda (e)
(printf "Key handler got ~e~n" e))]
[else #f]))
(super-new))
f))
(send f show #t))