diff --git a/termkey.c b/termkey.c index b8f06e1..4e0bb5a 100644 --- a/termkey.c +++ b/termkey.c @@ -824,6 +824,20 @@ const char *termkey_get_keyname(termkey_t *tk, termkey_keysym sym) return "UNKNOWN"; } +termkey_keysym termkey_keyname2sym(termkey_t *tk, const char *keyname) +{ + /* We store an array, so we can't do better than a linear search. Doesn't + * matter because user won't be calling this too often */ + + termkey_keysym sym; + + for(sym = 0; sym < tk->nkeynames; sym++) + if(tk->keynames[sym] && strcmp(keyname, tk->keynames[sym]) == 0) + return sym; + + return TERMKEY_SYM_UNKNOWN; +} + static termkey_keysym register_c0(termkey_t *tk, termkey_keysym sym, unsigned char ctrl, const char *name) { return register_c0_full(tk, sym, 0, 0, ctrl, name); diff --git a/termkey.h.in b/termkey.h.in index 95e8aed..cbf895d 100644 --- a/termkey.h.in +++ b/termkey.h.in @@ -156,6 +156,8 @@ termkey_result termkey_advisereadable(termkey_t *tk); termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym sym, const char *name); const char *termkey_get_keyname(termkey_t *tk, termkey_keysym sym); +termkey_keysym termkey_keyname2sym(termkey_t *tk, const char *keyname); + typedef enum { TERMKEY_FORMAT_LONGMOD = 1 << 0, // Shift-... instead of S-... TERMKEY_FORMAT_CARETCTRL = 1 << 1, // ^X instead of C-X diff --git a/termkey_get_keyname.3 b/termkey_get_keyname.3 index c1f0d79..62dc1d2 100644 --- a/termkey_get_keyname.3 +++ b/termkey_get_keyname.3 @@ -10,11 +10,12 @@ termkey_get_keyname \- return a string name for a symbolic key .sp Link with \fI-ltermkey\fP. .SH DESCRIPTION -\fBtermkey_get_keyname\fP returns a human-readable string name for the symbolic key value given by \fBsym\fP. The returned string is owned by the termkey instance \fItk\fP so should not be modified or freed. The returned pointer is guaranteed to be valid until the termkey instance is released using \fBtermkey_destroy\fP(3). +\fBtermkey_get_keyname\fP returns a human-readable string name for the symbolic key value given by \fBsym\fP. The returned string is owned by the termkey instance \fItk\fP so should not be modified or freed. The returned pointer is guaranteed to be valid until the termkey instance is released using \fBtermkey_destroy\fP(3). This function is the inverse of \fBtermkey_keyname2sym\fP(3). .SH "RETURN VALUE" \fBtermkey_get_key\fP() returns a pointer to a string. .SH "SEE ALSO" .BR termkey_new (3), .BR termkey_getkey (3), .BR termkey_waitkey (3), +.BR termkey_keyname2sym (3), .BR termkey_snprint_key (3) diff --git a/termkey_keyname2sym.3 b/termkey_keyname2sym.3 new file mode 100644 index 0000000..aba22f4 --- /dev/null +++ b/termkey_keyname2sym.3 @@ -0,0 +1,21 @@ +.TH TERMKEY_KEYNAME2SYM 3 +.SH NAME +termkey_keyname2sym \- look up a symbolic key value for a string name +.SH SYNOPSIS +.nf +.B #include +.sp +.BI "termkey_keysym termkey_keyname2sym(termkey_t *" tk ", const char *" keyname ); +.fi +.sp +Link with \fI-ltermkey\fP. +.SH DESCRIPTION +\fBtermkey_keyname2sym\fP looks up the symbolic key value represented by the given string name. This is a case-sensitive comparison. If the given name is not found, \fBTERMKEY_SYM_UNKNOWN\fP is returned instead. This function is the inverse of \fBtermkey_get_keyname\fP(3). +.PP +Because the key names are stored in an array indexed by the symbol number, this function has to perform a linear search of the names. Use of this function should be restricted to converting key names into symbolic values during a program's initialisation, so that efficient comparisons can be done while it is running. +.SH "RETURN VALUE" +\fBtermkey_get_key\fP() returns a pointer to a string. +.SH "SEE ALSO" +.BR termkey_new (3), +.BR termkey_get_keyname (3), +.BR termkey_snprint_key (3)