]> pd.if.org Git - pd_readline/blob - keyhandler.c
Very good progress.
[pd_readline] / keyhandler.c
1
2
3 /*  keyhandler.c  */  
4
5 /*                                                     
6   A simple program to handle keys (in particular, function keys, 
7   Ctrl keys, Alt keys and arrow keys). 
8   This code is released to the public domain. 
9   "Share and enjoy...."   ;)  
10 */                    
11
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <termios.h>
17 #include <ctype.h> 
18
19
20 void func1(void)
21 {  
22    printf("Hey, you entered foo! \n"); 
23
24
25 void func2(void) 
26 {  
27    printf("Hey, you entered bar! \n"); 
28
29      
30 void alt_a(void)
31
32    printf("Hey, you pressed ALT a! \n"); 
33 }   
34
35 void alt_b(void)
36
37    printf("Hey, you pressed ALT b! \n"); 
38 }   
39
40 void ctrl_a(void)
41
42    printf("Hey, you pressed CTRL a! \n"); 
43 }   
44
45 void ctrl_g(void)
46
47    printf("Hey, you pressed CTRL g! \n"); 
48 }   
49
50 void up_arrow(void)
51
52    printf("Hey, you pressed the up arrow! \n"); 
53 }   
54
55 void down_arrow(void)
56
57    printf("Hey, you pressed the down arrow! \n"); 
58 }
59
60 void left_arrow(void)
61
62    printf("Hey, you pressed the left arrow! \n"); 
63 }
64
65 void right_arrow(void)
66
67    printf("Hey, you pressed the right arrow! \n"); 
68
69
70 void f2(void)
71
72    printf("Hey, you pressed F2! \n"); 
73
74
75 void f3(void)
76
77    printf("Hey, you pressed F3! \n"); 
78
79
80 void f4(void)
81
82    printf("Hey, you pressed F4! \n"); 
83
84
85
86
87     
88 int main(void) 
89
90   
91   char word[80]; 
92   char ch; 
93   
94   do { 
95     puts("Enter some text :"); 
96     scanf("%s", word);  
97     
98    if ( !strcmp(word, "foo") ) { 
99       func1(); 
100   } 
101   
102    else if (!strcmp(word, "bar") ) { 
103       func2(); 
104   } 
105   
106    else if (!strcmp(word, "\x1b\x61") ) { 
107       alt_a(); 
108   } 
109   
110    else if (!strcmp(word, "\x1b\x62") ) { 
111       alt_b(); 
112   } 
113     
114    else if (!strcmp(word, "\x07") ) { 
115       ctrl_g(); 
116   } 
117   
118    else if (!strcmp(word, "\x01") ) { 
119       ctrl_a(); 
120   }
121       
122    else if (!strcmp(word, "\x1b\x5b\x41") ) { 
123       up_arrow(); 
124   }   
125       
126    else if (!strcmp(word, "\x1b\x5b\x42") ) { 
127       down_arrow(); 
128   }    
129       
130    else if (!strcmp(word, "\x1b\x5b\x43") ) { 
131       right_arrow(); 
132   }   
133    
134    else if (!strcmp(word, "\x1b\x5b\x44") ) { 
135       left_arrow(); 
136   } 
137          
138    else if (!strcmp(word, "\x1b\x4f\x51") ) { 
139       f2(); 
140   } 
141    
142    else if (!strcmp(word, "\x1b\x4f\x52") ) { 
143       f3(); 
144   }
145   
146    else if (!strcmp(word, "\x1b\x4f\x53") ) { 
147       f4(); 
148   }                    
149                                                                                               
150    else  { 
151    printf("Nope - I do not recognise that phrase.... \n"); 
152   }     
153   
154    printf("Try again? (y/n) : "); 
155    scanf(" %c%*c", &ch);  
156   } 
157   
158     while( toupper(ch) != 'N' );  
159        
160   return 0; 
161
162
163
164
165