From 7b782d8f7d2dc98e9dccd3b88f6793a0c2b20aca Mon Sep 17 00:00:00 2001 From: Justin Lovinger Date: Tue, 10 Feb 2026 22:37:44 +0000 Subject: [PATCH] Fix keys with multi-character labels causing issues with swiping Signed-off-by: Maarten van Gompel --- keyboard.c | 33 +++++++++++++++++++++++++++++---- keyboard.h | 1 + 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/keyboard.c b/keyboard.c index 7e407e3..4903399 100644 --- a/keyboard.c +++ b/keyboard.c @@ -553,9 +553,13 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) void kbd_print_key_stdout(struct kbd *kb, struct key *k) { - /* printed keys may slightly differ from the actual output + /* Printed keys may slightly differ from the actual output * we generally print what is on the key LABEL and only support the normal - * and shift layers. Other modifiers produce no output (Ctrl,Alt) + * and shift layers. Other modifiers produce no output (Ctrl,Alt). + * If a label has more than one character, + * only the first character is printed, + * because downpipe programs otherwise have no way of differentiating a swipe + * from a single keypress. * */ bool handled = true; @@ -584,13 +588,34 @@ kbd_print_key_stdout(struct kbd *kb, struct key *k) if (!handled) { if ((kb->mods & Shift) || ((kb->mods & CapsLock) & (strlen(k->label) == 1 && isalpha(k->label[0])))) - printf("%s", k->shift_label); + kbd_print_first_utf8_char_stdout(k->shift_label); else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super)) - printf("%s", k->label); + kbd_print_first_utf8_char_stdout(k->label); } fflush(stdout); } +void kbd_print_first_utf8_char_stdout(const char *str) { + unsigned char c = (unsigned char)str[0]; + int len; + + if ((c & 0x80) == 0) { + len = 1; + } else if ((c & 0xE0) == 0xC0) { + len = 2; + } else if ((c & 0xF0) == 0xE0) { + len = 3; + } else if ((c & 0xF8) == 0xF0) { + len = 4; + } else { + len = 1; // Invalid UTF-8 + } + + for (int i = 0; i < len && str[i] != '\0'; i++) { + putchar(str[i]); + } +} + void kbd_clear_last_popup(struct kbd *kb) { diff --git a/keyboard.h b/keyboard.h index 3622da0..dc7e055 100644 --- a/keyboard.h +++ b/keyboard.h @@ -141,6 +141,7 @@ void kbd_release_key(struct kbd *kb, uint32_t time); void kbd_motion_key(struct kbd *kb, uint32_t time, uint32_t x, uint32_t y); void kbd_press_key(struct kbd *kb, struct key *k, uint32_t time); void kbd_print_key_stdout(struct kbd *kb, struct key *k); +void kbd_print_first_utf8_char_stdout(const char *str); void kbd_clear_last_popup(struct kbd *kb); void kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type); void kbd_draw_layout(struct kbd *kb);