2 * $Id: demo_defkey.c,v 1.13 2004/01/04 00:01:13 tom Exp $
4 * Demonstrate the define_key() function.
5 * Thomas Dickey - 2002/11/23
10 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
14 #define MY_LOGFILE "demo_defkey.log"
17 * Log the most recently-written line to our logfile
20 log_last_line(WINDOW
*win
)
26 if ((fp
= fopen(MY_LOGFILE
, "a")) != 0) {
29 n
= winnstr(win
, temp
, sizeof(temp
));
31 if (isspace(UChar(temp
[n
])))
37 fprintf(fp
, "%s\n", temp
);
43 * Convert a character to visible form.
53 } else if (ch
== '\033') {
55 } else if (ch
< ' ') {
56 sprintf(temp
, "\\%03o", ch
);
57 } else if (ch
>= 127) {
58 sprintf(temp
, "\\%03o", ch
);
60 sprintf(temp
, "%c", ch
);
66 * Convert a string to visible form.
69 visible(const char *string
)
76 if (string
!= 0 && *string
!= '\0') {
77 for (pass
= 0; pass
< 2; ++pass
) {
78 for (n
= 0; string
[n
] != '\0'; ++n
) {
80 strcpy(temp
, visichar(string
[n
]));
87 result
= (char *) calloc(need
, 1);
90 result
= (char *) calloc(1, 1);
96 really_define_key(WINDOW
*win
, const char *new_string
, int code
)
99 const char *code_name
= keyname(code
);
101 char *vis_string
= 0;
104 if (code_name
== 0) {
105 sprintf(temp
, "Keycode %d", code
);
109 if ((old_string
= keybound(code
, 0)) != 0) {
110 wprintw(win
, "%s is %s\n",
112 vis_string
= visible(old_string
));
114 wprintw(win
, "%s is not bound\n",
118 if (vis_string
!= 0) {
123 vis_string
= visible(new_string
);
124 if ((rc
= key_defined(new_string
)) > 0) {
125 wprintw(win
, "%s was bound to %s\n", vis_string
, keyname(rc
));
127 } else if (new_string
!= 0 && rc
< 0) {
128 wprintw(win
, "%s conflicts with longer strings\n", vis_string
);
131 rc
= define_key(new_string
, code
);
133 wprintw(win
, "%s unchanged\n", code_name
);
135 } else if (new_string
!= 0) {
136 wprintw(win
, "%s is now bound to %s\n",
140 } else if (old_string
!= 0) {
141 wprintw(win
, "%s deleted\n", code_name
);
144 if (vis_string
!= 0 && *vis_string
!= 0)
151 duplicate(WINDOW
*win
, NCURSES_CONST
char *name
, int code
)
153 char *value
= tigetstr(name
);
156 const char *prefix
= 0;
159 if (!strncmp(value
, "\033[", 2)) {
161 } else if (!strncmp(value
, "\033O", 2)) {
165 sprintf(temp
, "%s%s", prefix
, value
+ 2);
166 really_define_key(win
, temp
, code
);
172 redefine(WINDOW
*win
, char *string
, int code
)
174 really_define_key(win
, string
, code
);
178 remove_definition(WINDOW
*win
, int code
)
180 really_define_key(win
, 0, code
);
184 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
194 (void) cbreak(); /* take input chars one at a time, no wait for \n */
195 (void) noecho(); /* don't echo input */
197 printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
198 printw("adds duplicate definitions for cursor application and normal modes,\n");
199 printw("and removes any definitions for the mini keypad. Type any of those:\n");
202 win
= newwin(LINES
- 3, COLS
, 3, 0);
207 /* we do the define_key() calls after keypad(), since the first call to
208 * keypad() initializes the corresponding data.
210 for (n
= 0; n
< 12; ++n
) {
212 sprintf(name
, "kf%d", n
+ 1);
213 fkeys
[n
] = tigetstr(name
);
215 for (n
= 0; n
< 12; ++n
) {
216 redefine(win
, fkeys
[11 - n
], KEY_F(n
+ 1));
219 duplicate(win
, "kcub1", KEY_LEFT
);
220 duplicate(win
, "kcuu1", KEY_UP
);
221 duplicate(win
, "kcud1", KEY_DOWN
);
222 duplicate(win
, "kcuf1", KEY_RIGHT
);
224 remove_definition(win
, KEY_A1
);
225 remove_definition(win
, KEY_A3
);
226 remove_definition(win
, KEY_B2
);
227 remove_definition(win
, KEY_C1
);
228 remove_definition(win
, KEY_C3
);
230 really_define_key(win
, "\033O", 1023);
232 while ((ch
= wgetch(win
)) != ERR
) {
233 const char *name
= keyname(ch
);
234 wprintw(win
, "Keycode %d, name %s\n",
236 name
!= 0 ? name
: "<null>");
247 printf("This program requires the ncurses library\n");
248 ExitProgram(EXIT_FAILURE
);