[racket-dev] get-x method on key event always returns zero?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Oct 9 22:40:26 EDT 2013


world uses get-x like this: 

      (define/public (deal-with-mouse %)
        (if (not on-mouse) 
            ;; No mouse handler => discard mouse events (so snip are not selected
            ;;  in the pasteboard, for example
            (class %
              (super-new)
              (define/override (on-event e)
                (void)))
            ;; Mouse handler => handle mouse events
            (class %
              (super-new)
              (define/override (on-event e)
                (define-values (x y me) (mouse-event->parts e))
                (when live
                  (cond
                    [(and (<= 0 x width) (<= 0 y height)) (pmouse x y me)]
                    [(member me '("leave" "enter")) (pmouse x y me)]
                    [else (void)]))))))

[[ Yes, this is a method-based mixin ]]

but note that I override on-event not on-key. The mouse-event->parts method uses get-x on e. -- Matthias



On Oct 9, 2013, at 5:17 PM, John Clements wrote:

> It appears to me that the 'get-x' method on a key event always returns zero, counter to what the docs say. Is this a doc bug, a software bug, or just me being dumb?
> 
> FWIW, here's a simple program that illustrates this; press a key while the window has focus, and you will always see 0 in the x value field:
> 
> #lang racket/base
> 
> 
> (require racket/gui
>         racket/class)
> 
> 
> (define sound-canvas%
>  (class canvas%
>    (init-field frame-num-text)
>    (init-field y-value-text)
> 
>    (define/override (on-char evt)
>      (send y-value-text begin-edit-sequence #f)
>      (send y-value-text erase)
>      (send y-value-text insert 
>            (format "x value: ~v"
>                    (send evt get-x)))
>      (send y-value-text end-edit-sequence)
>      (send frame-num-text begin-edit-sequence #f)
>      (send frame-num-text erase)
>      (send frame-num-text insert (format "key : ~a" (send evt get-key-code)))
>      (send frame-num-text end-edit-sequence))
> 
>    (super-new)))
> 
> 
> (let* ([f (new frame% [label "abc"] [width 400] [height 100])]
>       [tx (new text%)]
>       [ty (new text%)]
>       [c (new sound-canvas%
>               [parent f]
>               #;[paint-callback 
>                  (make-sound-drawing-callback left-getter right-getter
>                                               len data-left data-right)]
>               [frame-num-text tx]
>               [y-value-text   ty])]
>       [ecx (new editor-canvas%
>                 [parent f]
>                 [editor tx]
>                 [style '(no-border no-hscroll no-vscroll)]
>                 [stretchable-width #t]
>                 [stretchable-height #f]
>                 [horizontal-inset 1]
>                 [vertical-inset 1]
>                 [min-width 50]
>                 [min-height 20])]
>       [ecy (new editor-canvas%
>                 [parent f]
>                 [editor ty]
>                 [style '(no-border no-hscroll no-vscroll)]
>                 [stretchable-width #t]
>                 [stretchable-height #f]
>                 [horizontal-inset 1]
>                 [vertical-inset 1]
>                 [min-width 50]
>                 [min-height 20])])
>  (send f show #t))
> 
> 
> _________________________
>  Racket Developers list:
>  http://lists.racket-lang.org/dev



Posted on the dev mailing list.