X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=keyhandler.c;h=80ad5e5461f310dabafdc539c1d0a517fa638533;hb=3345932d0ab453d9ca85814fde1fe618bb36570f;hp=af0eb766ce54bc6aa85243a59859c16459b0ff6b;hpb=15155262dff7689ef3601df89e70d42ec3c127ca;p=pd_readline diff --git a/keyhandler.c b/keyhandler.c index af0eb76..80ad5e5 100644 --- a/keyhandler.c +++ b/keyhandler.c @@ -1,165 +1,78 @@ -/* keyhandler.c */ -/* - A simple program to handle keys (in particular, function keys, - Ctrl keys, Alt keys and arrow keys). - This code is released to the public domain. - "Share and enjoy...." ;) -*/ +/* keyhandler.c */ +/* Handle keystrokes for pd_readline. */ +/* This code is released to the public domain. */ +/* "Share and enjoy...." ;) */ +/* See the UNLICENSE file for details. */ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include "pd_readline.h" -void func1(void) -{ - printf("Hey, you entered foo! \n"); -} +/* This implementation of getch() is from here - */ +/* http://wesley.vidiqatch.org/ */ +/* Thanks, Wesley! */ +static struct termios old, new; -void func2(void) -{ - printf("Hey, you entered bar! \n"); -} - -void alt_a(void) -{ - printf("Hey, you pressed ALT a! \n"); -} - -void alt_b(void) -{ - printf("Hey, you pressed ALT b! \n"); -} - -void ctrl_a(void) -{ - printf("Hey, you pressed CTRL a! \n"); -} - -void ctrl_g(void) -{ - printf("Hey, you pressed CTRL g! \n"); -} +/* Initialize new terminal i/o settings */ +void initTermios(int echo) { + tcgetattr(0, &old); /* grab old terminal i/o settings */ + new = old; /* make new settings same as old settings */ + new.c_lflag &= ~ICANON; /* disable buffered i/o */ + new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ + tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */ +} -void up_arrow(void) -{ - printf("Hey, you pressed the up arrow! \n"); -} -void down_arrow(void) -{ - printf("Hey, you pressed the down arrow! \n"); +/* Restore old terminal i/o settings */ +void resetTermios(void) { + tcsetattr(0, TCSANOW, &old); } -void left_arrow(void) -{ - printf("Hey, you pressed the left arrow! \n"); + +/* Read 1 character - echo defines echo mode */ +char getch_(int echo) { + char ch; + initTermios(echo); + ch = getchar(); + resetTermios(); + return ch; } -void right_arrow(void) -{ - printf("Hey, you pressed the right arrow! \n"); -} -void f2(void) -{ - printf("Hey, you pressed F2! \n"); -} +/* Read 1 character without echo */ +char getch(void) { + return getch_(0); +} -void f3(void) -{ - printf("Hey, you pressed F3! \n"); -} -void f4(void) -{ - printf("Hey, you pressed F4! \n"); +/* Read 1 character with echo */ +char getche(void) { + return getch_(1); } - -int main(void) +void keyhandler(void) { + int i = getch(); - char word[80]; - char ch; - - do { - puts("Enter some text :"); - scanf("%s", word); - - if ( !strcmp(word, "foo") ) { - func1(); - } - - else if (!strcmp(word, "bar") ) { - func2(); - } - - else if (!strcmp(word, "\x1b\x61") ) { - alt_a(); - } - - else if (!strcmp(word, "\x1b\x62") ) { - alt_b(); - } - - else if (!strcmp(word, "\x07") ) { - ctrl_g(); - } - - else if (!strcmp(word, "\x01") ) { - ctrl_a(); - } - - else if (!strcmp(word, "\x1b\x5b\x41") ) { - up_arrow(); + switch(i) + { + case (27): puts("1"); /* escape() ; */ + case (33): puts("2"); /* dosomething(); */ + case (42): puts("3"); /* something(); */ + default: puts("4"); /* stuff(); */ } - - else if (!strcmp(word, "\x1b\x5b\x42") ) { - down_arrow(); - } - - else if (!strcmp(word, "\x1b\x5b\x43") ) { - right_arrow(); - } - - else if (!strcmp(word, "\x1b\x5b\x44") ) { - left_arrow(); - } - - else if (!strcmp(word, "\x1b\x4f\x51") ) { - f2(); - } - - else if (!strcmp(word, "\x1b\x4f\x52") ) { - f3(); - } - - else if (!strcmp(word, "\x1b\x4f\x53") ) { - f4(); - } - - else { - printf("Nope - I do not recognise that phrase.... \n"); - } - printf("Try again? (y/n) : "); - scanf(" %c%*c", &ch); - } - - while( toupper(ch) != 'N' ); - - return 0; - } - + +