Fix handling of ctrl key in legacy mode

Also change the glfw constants used for the modifiers to match those
used in the terminal encoding. Less likely to make mistakes translating
that way.
This commit is contained in:
Kovid Goyal
2021-01-14 16:05:07 +05:30
parent 39f41faf9f
commit 0714fd376b
8 changed files with 271 additions and 95 deletions

View File

@@ -206,7 +206,7 @@ encode_function_key(const KeyEvent *ev, char *output) {
}
static inline char
shifted_ascii_key(const uint32_t key) {
shifted_ascii_key(const uint32_t key) { // {{{
switch(key) {
/* start shifted key map (auto generated by gen-key-constants.py do not edit) */
case '`': return '~';
@@ -258,25 +258,80 @@ shifted_ascii_key(const uint32_t key) {
case 'z': return 'Z';
/* end shifted key map */
default:
return 0;
return key;
}
}
} // }}}
static char
ctrled_key(const uint32_t key) { // {{{
switch(key) {
/* start ctrl mapping (auto generated by gen-key-constants.py do not edit) */
case ' ': return 0;
case '/': return 31;
case '0': return 48;
case '1': return 49;
case '2': return 0;
case '3': return 27;
case '4': return 28;
case '5': return 29;
case '6': return 30;
case '7': return 31;
case '8': return 127;
case '9': return 57;
case '?': return 127;
case '@': return 0;
case '[': return 27;
case '\\': return 28;
case ']': return 29;
case '^': return 30;
case '_': return 31;
case 'a': return 1;
case 'b': return 2;
case 'c': return 3;
case 'd': return 4;
case 'e': return 5;
case 'f': return 6;
case 'g': return 7;
case 'h': return 8;
case 'i': return 9;
case 'j': return 10;
case 'k': return 11;
case 'l': return 12;
case 'm': return 13;
case 'n': return 14;
case 'o': return 15;
case 'p': return 16;
case 'q': return 17;
case 'r': return 18;
case 's': return 19;
case 't': return 20;
case 'u': return 21;
case 'v': return 22;
case 'w': return 23;
case 'x': return 24;
case 'y': return 25;
case 'z': return 26;
case '~': return 30;
/* end ctrl mapping */
default:
return key;
}
} // }}}
static int
encode_printable_ascii_key_legacy(const KeyEvent *ev, char *output) {
if (!ev->mods.value) return snprintf(output, KEY_BUFFER_SIZE, "%c", (char)ev->key);
if (ev->disambiguate) return 0;
char shifted_key = shifted_ascii_key(ev->key);
shifted_key = (shifted_key && ev->mods.shift) ? shifted_key : (char)ev->key;
char shifted_key = (ev->mods.shift) ? shifted_ascii_key(ev->key) : (char)ev->key;
if (ev->mods.value == SHIFT)
return snprintf(output, KEY_BUFFER_SIZE, "%c", shifted_key);
if ((ev->mods.value == ALT || ev->mods.value == (SHIFT | ALT)))
return snprintf(output, KEY_BUFFER_SIZE, "\x1b%c", shifted_key);
if (ev->mods.value == CTRL)
return snprintf(output, KEY_BUFFER_SIZE, "%c", ev->key & 0x3f);
return snprintf(output, KEY_BUFFER_SIZE, "%c", ctrled_key(ev->key));
if (ev->mods.value == (CTRL | ALT))
return snprintf(output, KEY_BUFFER_SIZE, "\x1b%c", ev->key & 0x3f);
return snprintf(output, KEY_BUFFER_SIZE, "\x1b%c", ctrled_key(ev->key));
return 0;
}