ntk-chtheme: Add new color scheme. Save/restore selection color.
[ntk.git] / test / keyboard.cxx
blob6ecc3d12735cfda37f36964b4b426f3974c5f350
1 //
2 // "$Id: keyboard.cxx 7978 2010-12-08 14:00:35Z AlbrechtS $"
3 //
4 // Keyboard/event test program for the Fast Light Tool Kit (FLTK).
5 //
6 // Continuously display FLTK's event state.
7 //
8 // Known bugs:
9 //
10 // X insists on reporting the state *before* the shift key was
11 // pressed, rather than after, on shift key events. I fixed this for
12 // the mouse buttons, but it did not seem worth it for shift.
14 // X servers do not agree about any shift flags after except shift, ctrl,
15 // lock, and alt. They may also not agree about the symbols for the extra
16 // keys Micro$oft put on the keyboard.
18 // On IRIX the backslash key does not work. A bug in XKeysymToKeycode?
20 // Copyright 1998-2010 by Bill Spitzak and others.
22 // This library is free software; you can redistribute it and/or
23 // modify it under the terms of the GNU Library General Public
24 // License as published by the Free Software Foundation; either
25 // version 2 of the License, or (at your option) any later version.
27 // This library is distributed in the hope that it will be useful,
28 // but WITHOUT ANY WARRANTY; without even the implied warranty of
29 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 // Library General Public License for more details.
32 // You should have received a copy of the GNU Library General Public
33 // License along with this library; if not, write to the Free Software
34 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 // USA.
37 // Please report all bugs and problems on the following page:
39 // http://www.fltk.org/str.php
43 #include "keyboard_ui.h"
45 #include <string.h>
48 // these are used to identify which buttons are which:
49 void key_cb(Fl_Button*, void*) {}
50 void shift_cb(Fl_Button*, void*) {}
51 void wheel_cb(Fl_Dial*, void*) {}
53 // this is used to stop Esc from exiting the program:
54 int handle(int e) {
55 return (e == FL_SHORTCUT); // eat all keystrokes
58 int MyWindow::handle(int msg) {
59 if (msg==FL_MOUSEWHEEL)
61 roller_x->value( roller_x->value() + Fl::e_dx * roller_x->step() );
62 roller_y->value( roller_y->value() + Fl::e_dy * roller_y->step() );
63 return 1;
65 return 0;
68 struct keycode_table{int n; const char* text;} table[] = {
69 {FL_Escape, "FL_Escape"},
70 {FL_BackSpace, "FL_BackSpace"},
71 {FL_Tab, "FL_Tab"},
72 {FL_Enter, "FL_Enter"},
73 {FL_Print, "FL_Print"},
74 {FL_Scroll_Lock, "FL_Scroll_Lock"},
75 {FL_Pause, "FL_Pause"},
76 {FL_Insert, "FL_Insert"},
77 {FL_Home, "FL_Home"},
78 {FL_Page_Up, "FL_Page_Up"},
79 {FL_Delete, "FL_Delete"},
80 {FL_End, "FL_End"},
81 {FL_Page_Down, "FL_Page_Down"},
82 {FL_Left, "FL_Left"},
83 {FL_Up, "FL_Up"},
84 {FL_Right, "FL_Right"},
85 {FL_Down, "FL_Down"},
86 {FL_Shift_L, "FL_Shift_L"},
87 {FL_Shift_R, "FL_Shift_R"},
88 {FL_Control_L, "FL_Control_L"},
89 {FL_Control_R, "FL_Control_R"},
90 {FL_Caps_Lock, "FL_Caps_Lock"},
91 {FL_Alt_L, "FL_Alt_L"},
92 {FL_Alt_R, "FL_Alt_R"},
93 {FL_Meta_L, "FL_Meta_L"},
94 {FL_Meta_R, "FL_Meta_R"},
95 {FL_Menu, "FL_Menu"},
96 {FL_Help, "FL_Help"},
97 {FL_Num_Lock, "FL_Num_Lock"},
98 {FL_KP_Enter, "FL_KP_Enter"}
101 int main(int argc, char** argv) {
102 Fl::add_handler(handle);
103 MyWindow *window = make_window();
104 window->show(argc,argv);
105 while (Fl::wait()) {
106 const char *str;
108 // update all the buttons with the current key and shift state:
109 for (int i = 0; i < window->children(); i++) {
110 Fl_Widget* b = window->child(i);
111 if (b->callback() == (Fl_Callback*)key_cb) {
112 int i = b->argument();
113 if (!i) i = b->label()[0];
114 Fl_Button *btn = ((Fl_Button*)b);
115 int state = Fl::event_key(i);
116 if (btn->value()!=state)
117 btn->value(state);
118 } else if (b->callback() == (Fl_Callback*)shift_cb) {
119 int i = b->argument();
120 Fl_Button *btn = ((Fl_Button*)b);
121 int state = Fl::event_state(i);
122 if (btn->value()!=state)
123 btn->value(state);
127 // figure out the keyname:
128 char buffer[100];
129 const char *keyname = buffer;
130 int k = Fl::event_key();
131 if (!k)
132 keyname = "0";
133 else if (k < 256) {
134 sprintf(buffer, "'%c'", k);
135 } else if (k > FL_F && k <= FL_F_Last) {
136 sprintf(buffer, "FL_F+%d", k - FL_F);
137 } else if (k >= FL_KP && k <= FL_KP_Last) {
138 sprintf(buffer, "FL_KP+'%c'", k-FL_KP);
139 } else if (k >= FL_Button && k <= FL_Button+7) {
140 sprintf(buffer, "FL_Button+%d", k-FL_Button);
141 } else {
142 sprintf(buffer, "0x%04x", k);
143 for (int i = 0; i < int(sizeof(table)/sizeof(*table)); i++)
144 if (table[i].n == k) {keyname = table[i].text; break;}
146 if (strcmp(key_output->value(), keyname))
147 key_output->value(keyname);
149 str = Fl::event_text();
150 if (strcmp(text_output->value(), str))
151 text_output->value(str);
153 return 0;
157 // End of "$Id: keyboard.cxx 7978 2010-12-08 14:00:35Z AlbrechtS $".