[racket] Key-event test problem‏

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri May 4 09:47:46 EDT 2012

This experiment suggests that you can: 

#lang racket/gui

(define frame (new frame% [label "hello world"][width 200][height 200]))

(define canvas
  (new (class canvas%
         (define/override (on-char evt)
           (define kc (send evt get-key-code))
           (define sd (send evt get-shift-down))
             [(and (eq? #\space kc) sd) (displayln "shift space bar")]
             [(eq? #\space kc) (displayln "only space bar")]
             [else (displayln "never mind")])))
       [parent frame]))

(send frame show #t)

-- Matthias

On May 4, 2012, at 6:50 AM, Laurine Harbulot wrote:

> We're sorry for our lack of precision.
> In fact, we're using the Racket/gui library and we would like to know how we can test if two keys are pressed at the same time.
> We're testing if the spacebar is pressed for the battleship shots and we're testing the left and right keys for the battleship moving.
> We want to know how we can test two keys at the same time, because we would like to allow moving and shot at the same time.
> We're working in a canvas with a modification on the function on-char, using a case on (send event get-key-code).
> This is our definition of the subclass based on canvas%:
> (define my-canvas%   
>   (class canvas%
>     (define/override (on-char evt)
>       (case (send evt get-key-code)
>         ((left) (when (and (> (- (send BATTLESHIP get-x) 5) 0)              ;;;Mov! es the battleship on the left
>                            (send BATTLESHIP alive?)
>                            (not game-over))
>                   (send BATTLESHIP move -8)))
>         ((right) (when (and (< (+ (send BATTLESHIP get-x) 35) LARG-JEU)      ;;;Moves the battleship on the left
>                             (send BATTLESHIP alive?)
>                ! ;             (not game-over))
>                    (send BATTLESHIP move 8)))
>         ((#\space) (when (and (empty? LIST-SHOT)        ;;;Battleship shot
>                               (send BATTLESHIP alive?)
>                               (not game-over))
>                      (begin (set! LIST-SHOT (cons (new shot% (x (+ (send BATTLESHIP get-x) 14))
>    ;           &nb! sp;                                         (y (- (send BATTLESHIP get-y) 20))
>                                                        (shooter 0))
>                                                   LIST-SHOT)))))
>         ((#\p) (i! f pause                                                        ;;;Pause for the game
>                    (begin (send TIMER start DT)
>                           (set! pause #f))
>                    (begin (send TIMER stop)
>                           (set! pause #t))))
> &nbs p;       ((escape) (begin (send TIMER st! op)                ;;;Quit game
>                          (send FRAME-GAME show #f)
>                          (send FRAME show #t)))
>         (else (send this on-paint))))
>     (super-new)))
> We would like to test (space and left) and (space and right).
> We hope we gave enough informations.
> Thank all.
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

Posted on the users mailing list.