You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.7KB

  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.
  13. //
  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.
  17. //
  18. // On IRIX the backslash key does not work. A bug in XKeysymToKeycode?
  19. //
  20. // Copyright 1998-2010 by Bill Spitzak and others.
  21. //
  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.
  26. //
  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.
  31. //
  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.
  36. //
  37. // Please report all bugs and problems on the following page:
  38. //
  39. // http://www.fltk.org/str.php
  40. //
  41. #include "keyboard_ui.h"
  42. #include <string.h>
  43. // these are used to identify which buttons are which:
  44. void key_cb(Fl_Button*, void*) {}
  45. void shift_cb(Fl_Button*, void*) {}
  46. void wheel_cb(Fl_Dial*, void*) {}
  47. // this is used to stop Esc from exiting the program:
  48. int handle(int e) {
  49. return (e == FL_SHORTCUT); // eat all keystrokes
  50. }
  51. int MyWindow::handle(int msg) {
  52. if (msg==FL_MOUSEWHEEL)
  53. {
  54. roller_x->value( roller_x->value() + Fl::e_dx * roller_x->step() );
  55. roller_y->value( roller_y->value() + Fl::e_dy * roller_y->step() );
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. struct keycode_table{int n; const char* text;} table[] = {
  61. {FL_Escape, "FL_Escape"},
  62. {FL_BackSpace, "FL_BackSpace"},
  63. {FL_Tab, "FL_Tab"},
  64. {FL_Enter, "FL_Enter"},
  65. {FL_Print, "FL_Print"},
  66. {FL_Scroll_Lock, "FL_Scroll_Lock"},
  67. {FL_Pause, "FL_Pause"},
  68. {FL_Insert, "FL_Insert"},
  69. {FL_Home, "FL_Home"},
  70. {FL_Page_Up, "FL_Page_Up"},
  71. {FL_Delete, "FL_Delete"},
  72. {FL_End, "FL_End"},
  73. {FL_Page_Down, "FL_Page_Down"},
  74. {FL_Left, "FL_Left"},
  75. {FL_Up, "FL_Up"},
  76. {FL_Right, "FL_Right"},
  77. {FL_Down, "FL_Down"},
  78. {FL_Shift_L, "FL_Shift_L"},
  79. {FL_Shift_R, "FL_Shift_R"},
  80. {FL_Control_L, "FL_Control_L"},
  81. {FL_Control_R, "FL_Control_R"},
  82. {FL_Caps_Lock, "FL_Caps_Lock"},
  83. {FL_Alt_L, "FL_Alt_L"},
  84. {FL_Alt_R, "FL_Alt_R"},
  85. {FL_Meta_L, "FL_Meta_L"},
  86. {FL_Meta_R, "FL_Meta_R"},
  87. {FL_Menu, "FL_Menu"},
  88. {FL_Help, "FL_Help"},
  89. {FL_Num_Lock, "FL_Num_Lock"},
  90. {FL_KP_Enter, "FL_KP_Enter"}
  91. };
  92. int main(int argc, char** argv) {
  93. Fl::add_handler(handle);
  94. MyWindow *window = make_window();
  95. window->show(argc,argv);
  96. while (Fl::wait()) {
  97. const char *str;
  98. // update all the buttons with the current key and shift state:
  99. for (int i = 0; i < window->children(); i++) {
  100. Fl_Widget* b = window->child(i);
  101. if (b->callback() == (Fl_Callback*)key_cb) {
  102. int i = b->argument();
  103. if (!i) i = b->label()[0];
  104. Fl_Button *btn = ((Fl_Button*)b);
  105. int state = Fl::event_key(i);
  106. if (btn->value()!=state)
  107. btn->value(state);
  108. } else if (b->callback() == (Fl_Callback*)shift_cb) {
  109. int i = b->argument();
  110. Fl_Button *btn = ((Fl_Button*)b);
  111. int state = Fl::event_state(i);
  112. if (btn->value()!=state)
  113. btn->value(state);
  114. }
  115. }
  116. // figure out the keyname:
  117. char buffer[100];
  118. const char *keyname = buffer;
  119. int k = Fl::event_key();
  120. if (!k)
  121. keyname = "0";
  122. else if (k < 256) {
  123. sprintf(buffer, "'%c'", k);
  124. } else if (k > FL_F && k <= FL_F_Last) {
  125. sprintf(buffer, "FL_F+%d", k - FL_F);
  126. } else if (k >= FL_KP && k <= FL_KP_Last) {
  127. sprintf(buffer, "FL_KP+'%c'", k-FL_KP);
  128. } else if (k >= FL_Button && k <= FL_Button+7) {
  129. sprintf(buffer, "FL_Button+%d", k-FL_Button);
  130. } else {
  131. sprintf(buffer, "0x%04x", k);
  132. for (int i = 0; i < int(sizeof(table)/sizeof(*table)); i++)
  133. if (table[i].n == k) {keyname = table[i].text; break;}
  134. }
  135. if (strcmp(key_output->value(), keyname))
  136. key_output->value(keyname);
  137. str = Fl::event_text();
  138. if (strcmp(text_output->value(), str))
  139. text_output->value(str);
  140. }
  141. return 0;
  142. }
  143. //
  144. // End of "$Id: keyboard.cxx 7978 2010-12-08 14:00:35Z AlbrechtS $".
  145. //