[racket-dev] multiple key-press

From: Paul Ojanen (engineer at alum.mit.edu)
Date: Fri Jul 23 11:52:00 EDT 2010

Here's an example to see what is detectable.  ALT and SHIFT are
interesting...try with lowercase first.

#lang racket
(require 2htdp/universe)
{require 2htdp/image}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Demonstrates how to detect multiple key presses               ;;
;;  - Only up to six or so keys are individually detectable      ;;
;;    -- my laptop does asdfjk                                   ;;
;;  - In some combinations, fewer individual keys are detectable ;;
;;    -- my laptop does not do qzc                               ;;
;;  - I think these limitations have to do with the hardware,    ;;
;;    so some keyboards may do better or worse                   ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; NOTE:  The ALT and SHIFT keys can break things in the current API
; Playing with them will generate some off but understandable behavior


; WorldState is a list of strings, indicating which keys are being held down
(define INIT_WORLD empty)

; key-handler: WorldState String -> WorldState
; purpose: Add key to the world without duplication
(define (key-handler ws key)
  (if (not (member key ws))
      (cons key ws)
      ws))

; release-handler: WorldState String -> WorldState
; purpose: Remove all instances of key from the world
(define (release-handler ws key)
  (if (member key ws)
      (release-handler (remove key ws) key)
      ws))

; render: WorldState -> Scene
; purpose: Provide a blank canvas in order to receive key events
(define (render ws)
  (rectangle 300 300 "solid" "white"))

(big-bang INIT_WORLD (on-key key-handler) (on-release release-handler)
(state true) (on-draw render))



> -----Original Message-----
> From: dev-bounces at racket-lang.org [mailto:dev-bounces at racket-lang.org] On
> Behalf Of Matthias Felleisen
> Sent: Friday, July 23, 2010 10:14 AM
> To: PLT Developers
> Subject: Re: [racket-dev] multiple key-press
> 
> 
> Thanks for all the ideas. Just to clarify: this spring I modified the API
> of Universe to accommodate this form of 'multiple' keypresses. I have to
> work out examples and probably add one more handler to get this 'right'
> (for beginners). (The person who posted isn't really a beginner in the
> sense of novice but a kid who mastered a good amount of Java and was set
> loose on the beginning languages. Perhaps he should have been thrown at
> full Racket from the beginning.)
> _________________________________________________
>   For list-related administrative tasks:
>   http://lists.racket-lang.org/listinfo/dev



Posted on the dev mailing list.