UP | HOME

NOTE: Racket GUI framework and editor component

framework based on racket/gui and provides some helpful components. This note is about editor, more precisely racket:text%.

Whole GUI system based on a class hierarchy, therefore, let's inherit racket:text%:

(define editor%
  (class racket:text%
    (init)
    (super-new)))

1. Keyboard Event

Override on-char can get key-event%:

(define editor%
  (class racket:text%
    (init)
    (super-new)
    (define/override (on-char e)
      (cond
        [(and (send e get-meta-down)
              (eq? (send e get-key-code) #\c))
         'copy-selected-text?]
        [(and (send e get-meta-down)
              (eq? (send e get-key-code) #\s))
         'save-to-file?]
        [else (super on-char e)]))))

An interesting thing was send key-event% get-x or send key-event% get-y won't get cursor position, but mouse position.

2. Mouse Event

Override on-local-event can get mouse-event%:

(define editor%
  (class racket:text%
    (init)
    (super-new)
    (define/override (on-local-event e)
      (cond
        [(and (send e get-meta-down)
              (send e button-down?))
         'jump-to-definition?]
        [else (super on-local-event e)]))))
Date: 2020-07-30 Thu 00:00
Author: Lîm Tsú-thuàn