-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.cxx
154 lines (144 loc) · 4.97 KB
/
keyboard.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Keyboard/event test program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2024 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
//
// Continuously display FLTK's event state.
//
// Known bugs:
//
// X insists on reporting the state *before* the shift key was
// pressed, rather than after, on shift key events. I fixed this for
// the mouse buttons, but it did not seem worth it for shift.
//
// X servers do not agree about any shift flags after except shift, ctrl,
// lock, and alt. They may also not agree about the symbols for the extra
// keys Micro$oft put on the keyboard.
//
// On IRIX the backslash key does not work. A bug in XKeysymToKeycode?
//
#include "keyboard_ui.h"
#include <string.h>
// these are used to identify which buttons are which:
void key_cb(Fl_Button*, void*) {}
void shift_cb(Fl_Button*, void*) {}
void wheel_cb(Fl_Dial*, void*) {}
// This is used to stop Esc from exiting the program.
// Other keystrokes like zoom keys (ctrl/+/-/0) must pass though.
int handle(int e) {
if (e == FL_SHORTCUT && Fl::event_key() == FL_Escape)
return 1;
return 0;
}
int MyWindow::handle(int msg) {
if (msg == FL_MOUSEWHEEL) {
roller_x->value( roller_x->value() + Fl::e_dx * roller_x->step() );
roller_y->value( roller_y->value() + Fl::e_dy * roller_y->step() );
return 1;
}
return 0;
}
struct keycode_table {
int n; // key code
const char* text; // key name
} key_table[] = {
{ FL_Escape, "FL_Escape"},
{ FL_BackSpace, "FL_BackSpace"},
{ FL_Tab, "FL_Tab"},
{ FL_Iso_Key, "FL_Iso_Key"},
{ FL_Enter, "FL_Enter"},
{ FL_Print, "FL_Print"},
{ FL_Scroll_Lock, "FL_Scroll_Lock"},
{ FL_Pause, "FL_Pause"},
{ FL_Insert, "FL_Insert"},
{ FL_Home, "FL_Home"},
{ FL_Page_Up, "FL_Page_Up"},
{ FL_Delete, "FL_Delete"},
{ FL_End, "FL_End"},
{ FL_Page_Down, "FL_Page_Down"},
{ FL_Left, "FL_Left"},
{ FL_Up, "FL_Up"},
{ FL_Right, "FL_Right"},
{ FL_Down, "FL_Down"},
{ FL_Shift_L, "FL_Shift_L"},
{ FL_Shift_R, "FL_Shift_R"},
{ FL_Control_L, "FL_Control_L"},
{ FL_Control_R, "FL_Control_R"},
{ FL_Caps_Lock, "FL_Caps_Lock"},
{ FL_Alt_L, "FL_Alt_L"},
{ FL_Alt_R, "FL_Alt_R"},
{ FL_Meta_L, "FL_Meta_L"},
{ FL_Meta_R, "FL_Meta_R"},
{ FL_Menu, "FL_Menu"},
{ FL_Help, "FL_Help"},
{ FL_Num_Lock, "FL_Num_Lock"},
{ FL_KP_Enter, "FL_KP_Enter"},
{ FL_Alt_Gr, "FL_Alt_Gr"}
};
int main(int argc, char** argv) {
Fl::add_handler(handle);
MyWindow *window = make_window();
window->show(argc,argv);
while (Fl::wait()) {
const char *str;
// update all the buttons with the current key and shift state:
for (int c = 0; c < window->children(); c++) {
Fl_Widget* b = window->child(c);
if (b->callback() == (Fl_Callback*)key_cb) {
int i = (int)b->argument();
if (!i) i = b->label()[0];
Fl_Button *btn = ((Fl_Button*)b);
int state = Fl::event_key(i);
if (btn->value()!=state)
btn->value(state);
} else if (b->callback() == (Fl_Callback*)shift_cb) {
int i = (int)b->argument();
Fl_Button *btn = ((Fl_Button*)b);
int state = Fl::event_state(i);
if (btn->value()!=state)
btn->value(state);
}
}
// figure out the keyname:
char buffer[100];
const char *keyname = buffer;
int k = Fl::event_key();
if (!k)
keyname = "0";
else if (k < 128) { // ASCII
snprintf(buffer, sizeof(buffer), "'%c'", k);
} else if (k >= 0xa0 && k <= 0xff) { // ISO-8859-1 (international keyboards)
char key[8];
int kl = fl_utf8encode((unsigned)k, key);
key[kl] = '\0';
snprintf(buffer, sizeof(buffer), "'%s'", key);
} else if (k > FL_F && k <= FL_F_Last) {
snprintf(buffer, sizeof(buffer), "FL_F+%d", k - FL_F);
} else if (k >= FL_KP && k <= FL_KP_Last) {
snprintf(buffer, sizeof(buffer), "FL_KP+'%c'", k-FL_KP);
} else if (k >= FL_Button && k <= FL_Button+7) {
snprintf(buffer, sizeof(buffer), "FL_Button+%d", k-FL_Button);
} else {
snprintf(buffer, sizeof(buffer), "0x%04x", k);
for (int i = 0; i < int(sizeof(key_table)/sizeof(*key_table)); i++)
if (key_table[i].n == k) {keyname = key_table[i].text; break;}
}
if (strcmp(key_output->value(), keyname))
key_output->value(keyname);
str = Fl::event_text();
if (strcmp(text_output->value(), str))
text_output->value(str);
}
return 0;
}