Racket GUI editor: how read and process keyboard input

31 views
Skip to first unread message

Christian Lykke

unread,
Dec 6, 2020, 8:17:19 AM12/6/20
to Racket Users

I would like to create an editor where I can use capslock
or some other key to input special commands
somewhat similar to M-x in emacs.

with capslock (since I never use capslock anyway):

every time I write someting in the editor, a function should
run that will do something with the input.
If capslock is turned off, the text should just be written
in the editor normally.
if capslock is on, then the text (letters, numbers ect)
should instead be written
in the field "commandField". if capslock is on and
the input is a space or cariagereturn,
then anoter function should run that takes the text
 in the "commandField" field as its argument and executes a command.
The command could be "increasesize" and
then the function would run "select all" and then
increase
the font size by 1, and finally clear the "commandField".
How could I do this?

with some other key:

It might be better to use another key than capslock,
for example a function key.
How could I create a boolean variable that
changes everytime a specific key (for example f2)
 gets pressed?

Thank you !

Here is the editor code I have so far:

#lang racket
(require racket/gui)

;first frame of the program
(define firstFrame (new frame% [label "An editor that allow you to input commands"]
                      [width 500]
                      [height 500]))

;the commands you type should be vissible in this message control
(define commandField (new message% [parent firstFrame]
                          [label "no commands yet..."]))

;the text area
(define mainCanvas (new editor-canvas% [parent firstFrame]))
(define mainTextArea (new text%))
(send mainCanvas set-editor mainTextArea)

;A menu bar for ordinary texteditor functionality
(define manubar (new menu-bar% [parent firstFrame]))
(define editDropdown (new menu% [label "Edit"] [parent manubar]))
(define fontDropdown (new menu% [label "Font"] [parent manubar]))

(append-editor-operation-menu-items editDropdown #f)
(append-editor-font-menu-items fontDropdown)
(send mainTextArea set-max-undo-history 100)

(send firstFrame show #t)

Sorawee Porncharoenwase

unread,
Dec 6, 2020, 9:08:27 AM12/6/20
to Christian Lykke, Racket Users

IIRC, capslock can’t be handled by applications. When people customize capslock to do something else, what they actually do is to ask their OS to redirect the capslock keypress to other keys (say, F2), and then intercept these other keys instead.

To catch keypresses like F2, create your own class that has text% as a superclass, and override on-default-char.


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/f07ba9b2-df26-4402-bb97-722d2c4f8d4dn%40googlegroups.com.

Matthew Flatt

unread,
Dec 6, 2020, 11:14:53 AM12/6/20
to Racket Users, Christian Lykke, Sorawee Porncharoenwase
Although key events from `racket/gui` cannot report the press of Caps
Lock as an independent event, each key event reports whether Caps Lock
is currently on, which I think is relevant here.

For example, using

(define mainTextArea (new (class text%
(define/override (on-char e)
(when (send e get-caps-down)
(bell))
(super on-char e))
(super-new))))

will play a sound every time there's a key press with Caps Lock
reported, and not affect the result otherwise.

Unfortunately, I see that Caps Lock reporting is not consistent across
OSes. On Mac OS and other Unix variants, the event reports whether Caps
Lock mode is on (and that's the useful information here). On Windows,
the event reports whether the Caps Lock key is currently held down
(which is not so useful here).

I think `racket/gui` for Windows should probably change to report the
Caps Lock state instead of whether the key is being pressed. That would
be a backward-incompatible changes, though; it's possible that some
Racket program for Windows currently uses the keypress state of Caps
Lock like other modifier keys.

Matthew
> <https://groups.google.com/d/msgid/racket-users/f07ba9b2-df26-4402-bb97-722d2c4f
> 8d4dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> > .
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to racket-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CADcuegudKjEi_a_bnC3SvUnTkVwokiwL
> zL%2BqK8OVBK9GG-ofDQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages