从Racket中的键事件中获取键

时间:2017-04-13 17:23:01

标签: events keyboard scheme racket

我正在尝试使用代码来选择上/下/右/左键,修改http://docs.racket-lang.org/gui/windowing-overview.html上的示例

(define fr (new frame% [label "Testing"] [width 300] [height 300]))
(define msg (new message% [parent fr] [label ""]))

(define my-canvas%
  (class canvas% 
    (define/override (on-char key-event)
      (cond
        [(equal? key-event 'up) (send msg set-label "UP")]
        [(equal? key-event 'down) (send msg set-label "DOWN")]
        [(equal? key-event 'left) (send msg set-label "LEFT")]
        [(equal? key-event 'right) (send msg set-label "RIGHT")]
        [else (send msg set-label "Others")]))
    (super-new)))

(new my-canvas% [parent fr]
     [paint-callback
      (lambda (canvas dc)
        (send dc draw-ellipse 140 120 20 20))])

(send fr show #t)

密钥事件代码取自http://docs.racket-lang.org/gui/key-event_.html?q=event

然而,上/下/右/左键没有被正确识别,只有"其他"显示在消息中。问题在哪里以及如何纠正。

1 个答案:

答案 0 :(得分:0)

  1. (equal? key-event <key-code>)替换为(equal? (send key-event get-key-code) <key-code>)
  2. [stretchable-width #t]添加到msg的初始化参数列表中,否则您将无法看到输出。