1998-10-20 17:25:25 +04:00
|
|
|
//
|
2021-06-11 14:25:31 +03:00
|
|
|
// Keyboard/event test program for the Fast Light Tool Kit (FLTK).
|
2011-07-19 08:49:30 +04:00
|
|
|
//
|
2024-03-05 05:08:30 +03:00
|
|
|
// Copyright 1998-2024 by Bill Spitzak and others.
|
2011-07-19 08:49:30 +04:00
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2011-07-19 08:49:30 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2011-07-19 08:49:30 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2011-07-19 08:49:30 +04:00
|
|
|
//
|
|
|
|
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
// Continuously display FLTK's event state.
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
// Known bugs:
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
// 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.
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
// 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.
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
|
|
|
// On IRIX the backslash key does not work. A bug in XKeysymToKeycode?
|
|
|
|
//
|
2001-12-21 17:35:34 +03:00
|
|
|
|
2001-12-30 08:16:10 +03:00
|
|
|
#include "keyboard_ui.h"
|
2007-02-21 18:49:38 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// these are used to identify which buttons are which:
|
|
|
|
void key_cb(Fl_Button*, void*) {}
|
2001-12-21 17:35:34 +03:00
|
|
|
void shift_cb(Fl_Button*, void*) {}
|
2001-12-22 10:16:12 +03:00
|
|
|
void wheel_cb(Fl_Dial*, void*) {}
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2024-03-05 05:08:30 +03:00
|
|
|
// This is used to stop Esc from exiting the program.
|
|
|
|
// Other keystrokes like zoom keys (ctrl/+/-/0) must pass though.
|
1998-10-06 22:21:25 +04:00
|
|
|
int handle(int e) {
|
2024-03-05 05:08:30 +03:00
|
|
|
if (e == FL_SHORTCUT && Fl::event_key() == FL_Escape)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2001-12-21 17:35:34 +03:00
|
|
|
|
2023-08-05 17:37:38 +03:00
|
|
|
int MyWindow::handle(int msg) {
|
2024-03-09 16:03:51 +03:00
|
|
|
if (msg == FL_MOUSEWHEEL) {
|
2023-08-05 17:37:38 +03:00
|
|
|
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;
|
2001-12-21 17:35:34 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2024-03-09 16:03:51 +03:00
|
|
|
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"}
|
1998-10-06 22:21:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
Fl::add_handler(handle);
|
2009-04-21 13:09:37 +04:00
|
|
|
MyWindow *window = make_window();
|
1998-10-06 22:21:25 +04:00
|
|
|
window->show(argc,argv);
|
|
|
|
while (Fl::wait()) {
|
2007-02-21 18:49:38 +03:00
|
|
|
const char *str;
|
2020-07-01 19:03:10 +03:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// update all the buttons with the current key and shift state:
|
2019-02-03 00:29:53 +03:00
|
|
|
for (int c = 0; c < window->children(); c++) {
|
|
|
|
Fl_Widget* b = window->child(c);
|
1998-10-06 22:21:25 +04:00
|
|
|
if (b->callback() == (Fl_Callback*)key_cb) {
|
2023-02-02 22:54:19 +03:00
|
|
|
int i = (int)b->argument();
|
2020-07-01 19:03:10 +03:00
|
|
|
if (!i) i = b->label()[0];
|
2007-02-21 18:49:38 +03:00
|
|
|
Fl_Button *btn = ((Fl_Button*)b);
|
|
|
|
int state = Fl::event_key(i);
|
|
|
|
if (btn->value()!=state)
|
2020-07-01 19:03:10 +03:00
|
|
|
btn->value(state);
|
1998-10-06 22:21:25 +04:00
|
|
|
} else if (b->callback() == (Fl_Callback*)shift_cb) {
|
2023-02-02 22:54:19 +03:00
|
|
|
int i = (int)b->argument();
|
2007-02-21 18:49:38 +03:00
|
|
|
Fl_Button *btn = ((Fl_Button*)b);
|
|
|
|
int state = Fl::event_state(i);
|
|
|
|
if (btn->value()!=state)
|
2020-07-01 19:03:10 +03:00
|
|
|
btn->value(state);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// figure out the keyname:
|
|
|
|
char buffer[100];
|
|
|
|
const char *keyname = buffer;
|
|
|
|
int k = Fl::event_key();
|
|
|
|
if (!k)
|
|
|
|
keyname = "0";
|
2021-06-11 14:25:31 +03:00
|
|
|
else if (k < 128) { // ASCII
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(buffer, sizeof(buffer), "'%c'", k);
|
2021-06-11 14:25:31 +03:00
|
|
|
} else if (k >= 0xa0 && k <= 0xff) { // ISO-8859-1 (international keyboards)
|
|
|
|
char key[8];
|
|
|
|
int kl = fl_utf8encode((unsigned)k, key);
|
|
|
|
key[kl] = '\0';
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(buffer, sizeof(buffer), "'%s'", key);
|
2001-12-22 10:16:12 +03:00
|
|
|
} else if (k > FL_F && k <= FL_F_Last) {
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(buffer, sizeof(buffer), "FL_F+%d", k - FL_F);
|
1998-10-06 22:21:25 +04:00
|
|
|
} else if (k >= FL_KP && k <= FL_KP_Last) {
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(buffer, sizeof(buffer), "FL_KP+'%c'", k-FL_KP);
|
1998-10-06 22:21:25 +04:00
|
|
|
} else if (k >= FL_Button && k <= FL_Button+7) {
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(buffer, sizeof(buffer), "FL_Button+%d", k-FL_Button);
|
1998-10-06 22:21:25 +04:00
|
|
|
} else {
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(buffer, sizeof(buffer), "0x%04x", k);
|
2024-03-09 16:03:51 +03:00
|
|
|
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;}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2007-02-21 18:49:38 +03:00
|
|
|
if (strcmp(key_output->value(), keyname))
|
|
|
|
key_output->value(keyname);
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2007-02-21 18:49:38 +03:00
|
|
|
str = Fl::event_text();
|
|
|
|
if (strcmp(text_output->value(), str))
|
|
|
|
text_output->value(str);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|