#include #include #include Fl_Window* myWindow; Fl_Output* myOutputBox; Fl_Widget* keyWidget; class keyStrokes : public Fl_Widget { public: keyStrokes(); int handle(int event); virtual void draw() {;} }; keyStrokes::keyStrokes() : Fl_Widget( 0, 0, 0, 0 ) {;} int keyStrokes::handle(int event) { // Need to respond to FOCUS events in order to handle keyboard if (event == FL_FOCUS) return 1; if (event == FL_UNFOCUS) return 1; // Now define responses to KEY DOWN events if (event == FL_KEYBOARD) { switch ( Fl::event_key() ) { // Define all the responses separately // Here just a simple response, but this is where // you have to insert clever code or call to clever // code that does the work you want case 'y': myOutputBox->value("yes"); return 1; // to indicate we used the key event case 'n': myOutputBox->value("no"); return 1; // to indicate we used the key event case FL_Left: myOutputBox->value("left"); return 1; // to indicate we used the key event case FL_Right: myOutputBox->value("right"); return 1; // to indicate we used the key event case FL_Up: myOutputBox->value("up"); return 1; // to indicate we used the key event case FL_Down: myOutputBox->value("down"); return 1; // to indicate we used the key event } } // And we might also need to response to KEY release if (event == FL_KEYUP) { myOutputBox->value("keyup"); return 1; // to indicate we used the key event } return 0; // we had no interest in the event } int main() { myWindow = new Fl_Window( 300, 100, "FLTK Keypress" ); keyWidget = new keyStrokes( ); myOutputBox = new Fl_Output( 80, 30, 200, 30, "Press =" ); myWindow->end(); myWindow->show(); return Fl::run(); }