Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mechanism to perform conversions such as C-m/Return and C-i/Tab in a unified manner #1697

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions frontends/sdl2/keyboard.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,11 @@
(defun make-key (&key ctrl meta shift super sym)
(when (equal sym (string #\yen_sign))
(setf sym "\\"))
(cond ((and ctrl (equal sym "i"))
(lem:make-key :ctrl nil
:meta meta
:super super
:shift shift
:sym "Tab"))
((and ctrl (equal sym "m"))
(lem:make-key :ctrl nil
:meta meta
:super super
:shift shift
:sym "Return"))
(t
(lem:make-key :ctrl ctrl
:meta meta
:super super
:shift shift
:sym sym))))
(lem:make-key :ctrl ctrl
:meta meta
:super super
:shift shift
:sym sym))

(defstruct modifier
shift
Expand Down
33 changes: 32 additions & 1 deletion src/key.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@

(defmethod print-object ((object key) stream)
(with-slots (ctrl meta super hypher shift sym) object
(write-string (key-to-string :ctrl ctrl
:meta meta
:super super
:hypher hypher
:shift shift
:sym sym)
stream)))

(defun key-to-string (&key ctrl meta super hypher shift sym)
(with-output-to-string (stream)
(when hypher (write-string "H-" stream))
(when super (write-string "S-" stream))
(when meta (write-string "M-" stream))
Expand All @@ -38,10 +48,31 @@
(write-string "Space" stream)
(write-string sym stream))))

(defvar *key-conversions* '(("C-m" . "Return")
("C-i" . "Tab")
("C-[" . "Escape")))

(defvar *key-constructor-cache* (make-hash-table :test 'equal))

(defun convert-key (&rest args &key ctrl meta super hypher shift sym)
(let ((elt (assoc (apply #'key-to-string args) *key-conversions* :test #'equal)))
(if elt
(let ((key (first (parse-keyspec (cdr elt)))))
(list :ctrl (key-ctrl key)
:meta (key-meta key)
:super (key-super key)
:hypher (key-hypher key)
:shift (key-shift key)
:sym (key-sym key)))
(list :ctrl ctrl
:meta meta
:super super
:hypher hypher
:shift shift
:sym sym))))

(defun make-key (&rest args &key ctrl meta super hypher shift sym)
(let ((hashkey (list ctrl meta super hypher shift sym)))
(let ((hashkey (apply #'convert-key args)))
(or (gethash hashkey *key-constructor-cache*)
(setf (gethash hashkey *key-constructor-cache*)
(apply #'%make-key args)))))
Expand Down
Loading