5 /* Handle keystrokes for pd_readline. */
6 /* This code is released to the public domain. */
7 /* "Share and enjoy...." ;) */
8 /* See the UNLICENSE file for details. */
15 #include "pd_readline.h"
18 /* This implementation of getch() is from here - */
19 /* http://wesley.vidiqatch.org/ */
21 static struct termios old, new;
23 /* Initialize new terminal i/o settings */
24 void initTermios(int echo) {
25 tcgetattr(0, &old); /* grab old terminal i/o settings */
26 new = old; /* make new settings same as old settings */
27 new.c_lflag &= ~ICANON; /* disable buffered i/o */
28 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
29 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
33 /* Restore old terminal i/o settings */
34 void resetTermios(void) {
35 tcsetattr(0, TCSANOW, &old);
39 /* Read 1 character - echo defines echo mode */
40 char getch_(int echo) {
49 /* Read 1 character without echo */
55 /* Read 1 character with echo */
62 /* Arrow keys are esc [ A to esc [ D */
63 /* Alt keys are just esc then key (e.g. Alt-g is esc g ). */
64 /* Ctrl (then letter) keys are just Dec 1 to Dec 26 */
66 void keyhandler(buf b)
73 case (1): break; /* Ctrl a */
74 case (2): break; /* Ctrl b */
75 case (3): break; /* Ctrl c */
77 case (10): enter(); /* Enter */
79 case (27): spec(i); break; /* esc */
81 case (32): printf("%c", i); break; /* Printable chars. */
82 case (33): printf("%c", i); break; /* Printable chars. */
83 case (34): printf("%c", i); break; /* Printable chars. */
85 case (65): printf("%c", i); break; /* "A" */
86 case (66): printf("%c", i); break; /* "B" */
87 case (67): printf("%c", i); break; /* "C" */
88 case (68): printf("%c", i); break; /* "D" */
89 case (69): printf("%c", i); break; /* "E" */
90 case (70): printf("%c", i); break; /* "F" */
92 case (97): printf("%c", i); break; /* "a" */
93 case (98): printf("%c", i); break; /* "b" */
94 case (99): printf("%c", i); break; /* "c" */
95 case (100): printf("%c", i); break; /* "d" */
96 case (101): printf("%c", i); break; /* "e" */
97 case (102): printf("%c", i); break; /* "f" */
99 case (126): printf("%c", i); break; /* "~" */
101 case (127): delch(b); break;
103 default: break; /* stuff(); */