aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/showkey.c
diff options
context:
space:
mode:
Diffstat (limited to 'showkey.c')
-rw-r--r--showkey.c91
1 files changed, 56 insertions, 35 deletions
diff --git a/showkey.c b/showkey.c
index 2f5947b..47a1a3d 100644
--- a/showkey.c
+++ b/showkey.c
@@ -34,24 +34,6 @@ GLOBALS(
34#define TT this.showkey 34#define TT this.showkey
35 35
36 36
37// Callback for incoming CSI commands from the terminal.
38static void handleCSI(long extra, char *command, int *params, int count)
39{
40 int i;
41
42 // Is it a cursor location report?
43 if (strcmp("R", command) == 0)
44 {
45 printf("CSI cursor position - line %d, column %d\r\n", params[0], params[1]);
46 return;
47 }
48
49 printf("CSI command %s - ", command);
50 for (i = 0; i < count; i++)
51 printf("%d ", params[i]);
52 printf("\r\n");
53}
54
55static void quit() 37static void quit()
56{ 38{
57 printf("Quitting.\r\n"); 39 printf("Quitting.\r\n");
@@ -64,31 +46,70 @@ static struct keyCommand simpleKeys[] =
64 {"^C", quit} 46 {"^C", quit}
65}; 47};
66 48
67// Callback for incoming key sequences from the user. 49// Callback for incoming sequences from the terminal.
68static int handleKeySequence(long extra, char *sequence, int isTranslated) 50static int handleEvent(long extra, struct keyevent *event)
69{ 51{
70 int j, l = strlen(sequence); 52 int i;
71
72 if (isTranslated)
73 printf("TRANSLATED - ");
74 else
75 printf("KEY - ");
76 printf("%s\r\n", sequence);
77 53
78 // Search for a key sequence bound to a command. 54 switch (event->type)
79 for (j = 0; j < (sizeof(simpleKeys) / sizeof(*simpleKeys)); j++)
80 { 55 {
81 if (strncmp(simpleKeys[j].key, sequence, l) == 0) 56 case HK_RAW :
57 {
58 printf("RAW ");
59 for (i = 0; event->sequence[i]; i++)
60 {
61 printf("(%x) ", (int) event->sequence[i]);
62 if (32 > event->sequence[i])
63 printf("^%c, ", (int) event->sequence[i] + 'A' - 1);
64 else
65 printf("%c, ", (int) event->sequence[i]);
66 }
67 printf("-> ");
68 break;
69 }
70
71 case HK_KEYS :
82 { 72 {
83 // If it's a partial match, keep accumulating them. 73 int l = strlen(event->sequence);
84 if (strlen(simpleKeys[j].key) != l) 74
85 return 0; 75 if (event->isTranslated)
76 printf("TRANSLATED - ");
86 else 77 else
78 printf("KEY - ");
79 printf("%s\r\n", event->sequence);
80
81 // Search for a key sequence bound to a command.
82 for (i = 0; i < ARRAY_LEN(simpleKeys); i++)
83 {
84 if (strncmp(simpleKeys[i].key, event->sequence, l) == 0)
85 {
86 // If it's a partial match, keep accumulating them.
87 if (strlen(simpleKeys[i].key) != l)
88 return 0;
89 else
90 if (simpleKeys[i].handler) simpleKeys[i].handler();
91 }
92 }
93 break;
94 }
95
96 case HK_CSI :
97 {
98 // Is it a cursor location report?
99 if (strcmp("R", event->sequence) == 0)
87 { 100 {
88 if (simpleKeys[j].handler) simpleKeys[j].handler(); 101 printf("CSI cursor position - line %d, column %d\r\n", event->params[0], event->params[1]);
89 return 1; 102 return 1;
90 } 103 }
104
105 printf("CSI command %s - ", event->sequence);
106 for (i = 0; i < event->count; i++)
107 printf("%d ", event->params[i]);
108 printf("\r\n");
109 break;
91 } 110 }
111
112 default : break;
92 } 113 }
93 114
94 return 1; 115 return 1;
@@ -117,7 +138,7 @@ void showkey_main(void)
117 termIo.c_cc[VMIN]=1; 138 termIo.c_cc[VMIN]=1;
118 tcsetattr(0, TCSANOW, &termIo); 139 tcsetattr(0, TCSANOW, &termIo);
119 140
120 handle_keys(0, handleKeySequence, handleCSI); 141 handle_keys(0, handleEvent);
121 142
122 tcsetattr(0, TCSANOW, &oldTermIo); 143 tcsetattr(0, TCSANOW, &oldTermIo);
123 puts(""); 144 puts("");