[racket] Key events, shift, and, get-shift-key-code
Hi All,
Om my keyboard the . key wil produce an > if i press shift and . at
the same time.
How can I write a program that given . can produce > ?
I have experimented with the program below. It displays a canvas and
prints info on the key events received. My experiments with get-other-shift-code
has been unsuccessful. The documentation says that on OS X it works only
of cmd is pressed, but I can't get it to return anything but #f.
/Jens Axel
#lang racket
(require racket/gui)
; key-event->key : key-event -> string
; translate a key event into a "Emacs" style string
; Pressed Output
; a "a"
; Shift+a "A"
; Meta+a "M-a"
; Shift+Meta+a "M-A" ; <== todo returns "M-a" now
(define (key-event->key event)
(define shift? (send event get-shift-down))
(define c (send event get-key-code))
(define k (if shift?
(or (send event get-other-shift-key-code) c)
c))
(define ctrl? (send event get-control-down))
(define alt? (send event get-alt-down))
(define meta? (send event get-meta-down))
(let ([k (match k ['escape "ESC"] [_ k])])
(cond
[(or ctrl? alt? meta?) (~a (if ctrl? "C-" "")
(if alt? "A-" "")
(if meta? "M-" "")
k)]
[else k])))
; key-event/no-modifiers : make a copy of key-event with no modifiers set
(define (key-event/no-modifiers key-event)
(define e key-event)
(new key-event%
[key-code (send e get-key-code)]
[shift-down #f]
[control-down #f]
[meta-down #f]
[alt-down #f]
[x (send e get-x)]
[y (send e get-y)]
[time-stamp (send e get-time-stamp)]
[caps-down #f]
[mod3-down #f]
[mod4-down #f]
[mod5-down #f]
[control+meta-is-altgr #f]))
(define (get-shift-key-code key-event)
(define e (key-event/no-modifiers key-event))
(send e set-shift-down #t)
(send e get-other-shift-key-code))
(define frame (new frame% [label "Display Key Event"]))
(define msg (new message% [parent frame] [label "No news"]))
(send msg min-width 400)
(define key-canvas%
(class canvas%
(define/override (on-char event)
(define key (key-event->key event))
(unless (equal? key 'release)
(displayln (list 'key key 'shift (get-shift-key-code event)))
(send msg set-label (~a "key: " key " " "shift: "
(get-shift-key-code event)))))
(super-new)))
(define canvas (new key-canvas% [parent frame]))
(send canvas min-client-width 400)
(send canvas min-client-height 400)
(send msg set-label "Don't panic")
(send frame show #t)
--
--
Jens Axel Søgaard