1 /****************************************************************************
2 * Copyright (c) 2005-2006,2008 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: demo_altkeys.c,v 1.8 2008/04/12 22:06:22 tom Exp $
31 * Demonstrate the define_key() function.
32 * Thomas Dickey - 2005/10/22
35 #include <test.priv.h>
37 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
44 # include <sys/time.h>
50 #define MY_LOGFILE "demo_altkeys.log"
51 #define MY_KEYS (KEY_MAX + 1)
54 * Log the most recently-written line to our logfile
57 log_last_line(WINDOW *win)
63 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
64 int need = sizeof(temp) - 1;
69 n = winnstr(win, temp, need);
71 if (isspace(UChar(temp[n])))
77 fprintf(fp, "%s\n", temp);
83 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
89 struct timeval current, previous;
94 newterm(0, stdout, stdin);
95 (void) cbreak(); /* take input chars one at a time, no wait for \n */
96 (void) noecho(); /* don't echo input */
98 scrollok(stdscr, TRUE);
102 /* we do the define_key() calls after keypad(), since the first call to
103 * keypad() initializes the corresponding data.
105 for (n = 0; n < 255; ++n) {
107 sprintf(temp, "\033%c", n);
108 define_key(temp, n + MY_KEYS);
110 for (n = KEY_MIN; n < KEY_MAX; ++n) {
112 if ((value = keybound(n, 0)) != 0) {
113 char *temp = typeMalloc(char, strlen(value) + 2);
114 sprintf(temp, "\033%s", value);
115 define_key(temp, n + MY_KEYS);
121 #if HAVE_GETTIMEOFDAY
122 gettimeofday(&previous, 0);
125 while ((ch = getch()) != ERR) {
126 bool escaped = (ch >= MY_KEYS);
127 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
129 #if HAVE_GETTIMEOFDAY
130 gettimeofday(¤t, 0);
131 secs = current.tv_sec - previous.tv_sec;
132 msecs = (current.tv_usec - previous.tv_usec) / 1000;
138 secs += msecs / 1000;
141 printw("%6d.%03d ", secs, msecs);
144 printw("Keycode %d, name %s%s\n",
146 escaped ? "ESC-" : "",
147 name != 0 ? name : "<null>");
148 log_last_line(stdscr);
154 ExitProgram(EXIT_SUCCESS);
160 printf("This program requires the ncurses library\n");
161 ExitProgram(EXIT_FAILURE);