From 57cdf605f583f84190536042ebcbb96e8856da8b Mon Sep 17 00:00:00 2001 From: Nulo Date: Mon, 13 Dec 2021 17:13:56 -0300 Subject: [PATCH] Sticky keys --- keycodes.scm | 3 +++ menelaus.scm | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/keycodes.scm b/keycodes.scm index 0d2c957..39234d4 100644 --- a/keycodes.scm +++ b/keycodes.scm @@ -1,5 +1,8 @@ ;; port of usb_keyboard.h +; Actually ErrorUndefined +(define sticky-modifier 3) + (define key-a 4) (define key-b 5) (define key-c 6) diff --git a/menelaus.scm b/menelaus.scm index 051209d..255a030 100644 --- a/menelaus.scm +++ b/menelaus.scm @@ -246,6 +246,7 @@ ;; Vectors to store keycodes for the USB frame we are preparing to send. (define modifiers (vector 0 0 0 0 0)) +(define sticky-modifiers (vector 0 0 0 0 0)) (define keycodes-down (vector 0 0 0 0 0 0)) ;; For each element of the keycodes-down or modifiers vector, which physical @@ -263,6 +264,11 @@ (vector-set! modifiers (- keycode 1) 1) (vector-set! keys-for-modifiers (- keycode 1) key)) +(define (press-sticky-modifier keycode) + (vector-set! sticky-modifiers (- keycode 1) 1)) +(define (release-sticky-modifier keycode) + (vector-set! sticky-modifiers (- keycode 1) 0)) + ;; Record that a given key resulted in a specific non-modifier press. (define (press-normal-key keycode key) (let ((slot (find keycodes-down 0))) @@ -271,17 +277,25 @@ ;; Record a key press in the modifiers/keycodes-down vectors for the layout. (define (press-key key) - (let ((keycode (lookup key))) + (let ((keycode (lookup key)) + (sticky (find sticky-modifiers 1))) ;; Sometimes "keycodes" are procedures; in that case we call them with ;; true when the key is pressed and false when it's released. (if (procedure? keycode) (keycode #t) (if (modifier? keycode) - (begin (press-modifier (unmodify keycode) key) - (if (uncombo keycode) - (press-normal-key (uncombo keycode) key) - #f)) - (press-normal-key keycode key))))) + (if (uncombo keycode) + (if (= (uncombo keycode) sticky-modifier) + (press-sticky-modifier (unmodify keycode)) + (begin + (press-modifier (unmodify keycode) key) + (press-normal-key (uncombo keycode) key))) + (press-modifier (unmodify keycode) key)) + (if sticky + (begin (press-modifier (+ sticky 1) key) + (press-normal-key keycode key) + (release-sticky-modifier (+ sticky 1))) + (press-normal-key keycode key)))))) ;; Record that a given key being released resulted in a modifier release. (define (release-modifier keycode key n)