| 1 | // |
| 2 | // Numeric keypad demo program for the Fast Light Tool Kit (FLTK). |
| 3 | // |
| 4 | // Copyright 1998-2024 by Bill Spitzak and others. |
| 5 | // |
| 6 | // This library is free software. Distribution and use rights are outlined in |
| 7 | // the file "COPYING" which should have been included with this file. If this |
| 8 | // file is missing or damaged, see the license at: |
| 9 | // |
| 10 | // https://www.fltk.org/COPYING.php |
| 11 | // |
| 12 | // Please see the following page on how to report bugs and issues: |
| 13 | // |
| 14 | // https://www.fltk.org/bugs.php |
| 15 | // |
| 16 | |
| 17 | // |
| 18 | // Demonstrate keyboard remapping: Force number pad to type numbers even if NumLock off |
| 19 | // |
| 20 | // DESCRIPTION |
| 21 | // ----------- |
| 22 | // This demonstrates several important things: |
| 23 | // |
| 24 | // o How to use the global event dispatcher function Fl::event_dispatch() |
| 25 | // |
| 26 | // o How to remap keyboard events |
| 27 | // |
| 28 | // o How to use Fl::event_original_key() to easily ensure the dual purpose |
| 29 | // numeric keypad keys generate only numbers even if NumLock is off, |
| 30 | // without affecting the dedicated Home/End/etc keys. |
| 31 | // |
| 32 | // Use of Fl::event_original_key() ensures someone hitting e.g. "7/Home" key |
| 33 | // returns FL_KP+'7', and not FL_Home, while allowing the dedicated "Home" key |
| 34 | // to still generate FL_Home. This allows users to use the "Home" and "End" |
| 35 | // keys to navigate the buffer, while "7/Home" and "1/End" always type numbers. |
| 36 | // |
| 37 | // This demo based on fltk.general thread entitled "keyboard mapping", Aug 2019. |
| 38 | // |
| 39 | |
| 40 | #include <FL/Fl.H> |
| 41 | #include <FL/Fl_Double_Window.H> |
| 42 | #include <FL/Fl_Input.H> |
| 43 | #include <FL/Fl_Check_Button.H> |
| 44 | |
| 45 | Fl_Check_Button *G_checkbut = 0; |
| 46 | |
| 47 | // Global event handler: FLTK calls this after event translation. It's up to us |
| 48 | // to call Fl::handle_(e,w) to actually deliver the event to the widgets. If we |
| 49 | // don't and just return, the event will be dropped. See docs for more. |
| 50 | |
| 51 | int MyHandler(int e, Fl_Window *w) { |
| 52 | // Remapping disabled? Early exit.. |
| 53 | if (G_checkbut->value() == 0) |
| 54 | return Fl::handle_(e, w); |
| 55 | // Keyboard key pressed? See if we should remap.. |
| 56 | if (e == FL_KEYDOWN || e == FL_KEYUP) { |
| 57 | // Get FLTK keycode /before/ NumLock state is applied (see above DESCRIPTION) |
| 58 | int keycode = Fl::event_original_key(); // get keycode before FLTK applies NumLock |
| 59 | if (keycode >= FL_KP && keycode <= FL_KP_Last) { // keypad key pressed? |
| 60 | static char buf[2]; // static: we don't want buffer to go out of scope |
| 61 | buf[0] = char(keycode - FL_KP); // convert keypad keycode -> ascii |
| 62 | buf[1] = 0; // terminate string (for safety) |
| 63 | Fl::e_text = buf; // point to our static buffer |
| 64 | Fl::e_length = 1; // only first char relevant |
| 65 | Fl::e_keysym = keycode; // note: some input widgets require this too |
| 66 | } |
| 67 | } |
| 68 | return Fl::handle_(e, w); // let FLTK deliver event to widgets |
| 69 | } |
| 70 | |
| 71 | int main(int argc, char *argv[]) { |
| 72 | Fl_Double_Window *win = new Fl_Double_Window(400, 150, "Keyboard Translation Test"); |
| 73 | win->begin(); |
| 74 | { |
| 75 | new Fl_Input(100, 10, 200, 25, "Input:"); |
| 76 | G_checkbut = new Fl_Check_Button(100, 40, 280, 25, "Force numeric keypad to type numbers"); |
| 77 | G_checkbut->labelsize(12); |
| 78 | G_checkbut->set(); |
| 79 | } |
| 80 | win->end(); |
| 81 | win->resizable(win); |
| 82 | win->show(argc, argv); |
| 83 | win->tooltip("Turn NumLock OFF, then type into Input:\nusing numeric keypad to test translation"); |
| 84 | // Set up our event handler to manage events |
| 85 | Fl::event_dispatch(MyHandler); |
| 86 | return(Fl::run()); |
| 87 | } |