Added termkey_snprint_key() to render nice string outputs of key events
This commit is contained in:
parent
95e87a86d5
commit
a06d010504
30
demo.c
30
demo.c
@ -3,36 +3,18 @@
|
||||
#include "termkey.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char buffer[50];
|
||||
termkey_t *tk = termkey_new(0, 0);
|
||||
|
||||
termkey_result ret;
|
||||
termkey_key key;
|
||||
|
||||
while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
|
||||
switch(key.type) {
|
||||
case TERMKEY_TYPE_KEYSYM:
|
||||
printf("Key %s%s%s%s (code %d)\n",
|
||||
key.modifiers & TERMKEY_KEYMOD_SHIFT ? "Shift-" : "",
|
||||
key.modifiers & TERMKEY_KEYMOD_ALT ? "Alt-" : "",
|
||||
key.modifiers & TERMKEY_KEYMOD_CTRL ? "Ctrl-" : "",
|
||||
termkey_get_keyname(tk, key.code.sym),
|
||||
key.code.sym);
|
||||
break;
|
||||
case TERMKEY_TYPE_FUNCTION:
|
||||
printf("Function key %s%s%sF%d\n",
|
||||
key.modifiers & TERMKEY_KEYMOD_SHIFT ? "Shift-" : "",
|
||||
key.modifiers & TERMKEY_KEYMOD_ALT ? "Alt-" : "",
|
||||
key.modifiers & TERMKEY_KEYMOD_CTRL ? "Ctrl-" : "",
|
||||
key.code.number);
|
||||
break;
|
||||
case TERMKEY_TYPE_UNICODE:
|
||||
printf("Unicode %s%s%s%s (U+%04X)\n",
|
||||
key.modifiers & TERMKEY_KEYMOD_SHIFT ? "Shift-" : "",
|
||||
key.modifiers & TERMKEY_KEYMOD_ALT ? "Alt-" : "",
|
||||
key.modifiers & TERMKEY_KEYMOD_CTRL ? "Ctrl-" : "",
|
||||
key.utf8,
|
||||
key.code.codepoint);
|
||||
break;
|
||||
if(key.type == TERMKEY_TYPE_UNICODE && !key.modifiers)
|
||||
printf("%s\n", key.utf8);
|
||||
else {
|
||||
termkey_snprint_key(tk, buffer, sizeof buffer, &key, 1);
|
||||
printf("<%s>\n", buffer);
|
||||
}
|
||||
|
||||
if(key.type == TERMKEY_TYPE_UNICODE &&
|
||||
|
41
termkey.c
41
termkey.c
@ -855,3 +855,44 @@ termkey_keysym termkey_register_csifunc_full(termkey_t *tk, termkey_type type, t
|
||||
|
||||
return sym;
|
||||
}
|
||||
|
||||
size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, int longmod)
|
||||
{
|
||||
size_t pos = 0;
|
||||
size_t l;
|
||||
|
||||
if(key->modifiers & TERMKEY_KEYMOD_CTRL) {
|
||||
l = snprintf(buffer + pos, len - pos, longmod ? "Ctrl-" : "C-");
|
||||
if(l <= 0) return pos;
|
||||
pos += l;
|
||||
}
|
||||
|
||||
if(key->modifiers & TERMKEY_KEYMOD_ALT) {
|
||||
l = snprintf(buffer + pos, len - pos, longmod ? "Alt-" : "A-");
|
||||
if(l <= 0) return pos;
|
||||
pos += l;
|
||||
}
|
||||
|
||||
if(key->modifiers & TERMKEY_KEYMOD_SHIFT) {
|
||||
l = snprintf(buffer + pos, len - pos, longmod ? "Shift-" : "S-");
|
||||
if(l <= 0) return pos;
|
||||
pos += l;
|
||||
}
|
||||
|
||||
switch(key->type) {
|
||||
case TERMKEY_TYPE_UNICODE:
|
||||
l = snprintf(buffer + pos, len - pos, "%s", key->utf8);
|
||||
break;
|
||||
case TERMKEY_TYPE_KEYSYM:
|
||||
l = snprintf(buffer + pos, len - pos, "%s", termkey_get_keyname(tk, key->code.sym));
|
||||
break;
|
||||
case TERMKEY_TYPE_FUNCTION:
|
||||
l = snprintf(buffer + pos, len - pos, "F%d", key->code.number);
|
||||
break;
|
||||
}
|
||||
|
||||
if(l <= 0) return pos;
|
||||
pos += l;
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
@ -132,4 +132,6 @@ termkey_keysym termkey_register_csi_ss3_full(termkey_t *tk, termkey_type type, t
|
||||
termkey_keysym termkey_register_ss3kpalt_full(termkey_t *tk, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name, char kpalt);
|
||||
termkey_keysym termkey_register_csifunc_full(termkey_t *tk, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, int number, const char *name);
|
||||
|
||||
size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, int longmod);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user