-1

I want to detect when enter is pressed in FL_INPUT and add its text to FL_Text_Display using C++

please help me I dont know what to do this is all I got

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Input.H>

using namespace std;

int main(int argc,char **argv) {

  Fl_Window win(320,240,"BIC");

  Fl_Text_Buffer txtbuf;

  win.begin();
    Fl_Text_Display ted(2,2,320-2,240-2-32-2);
    Fl_Input        inp(2,240-2-32,320-2,32);
  win.end();
  
  ted.buffer(txtbuf);
  
  win.resizable(ted);
  
  win.show();
  
  return Fl::run();
}

2 Answers2

0

You would have to inherit Fl_Input and override the virtual int handle(int) method.

class MyInput : public Fl_Input {
   public:   
    MyInput(int x, int y, int w, int h, const char* title=0) : Fl_Input(x, y, w, h, title) {}

   virtual int handle(int e) override {
        switch(e) {
            case FL_ENTER: foo(); return 1;
            default: return 0;
        }
    }
};
mo_al_
  • 561
  • 4
  • 9
0

Deriving a new class is possible but the OP's question is easier to solve with a callback. That's what callbacks are designed for. Modified example code follows:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Input.H>

// input callback (called when ENTER is pressed)
void input_cb(Fl_Widget *w, void *v) {
  Fl_Input *inp = (Fl_Input *)w;
  Fl_Text_Display *ted = (Fl_Text_Display *)v;
  Fl_Text_Buffer *tbuf = ted->buffer();
  tbuf->append(inp->value());
  tbuf->append("\n"); // append newline (optional)
  ted->insert_position(tbuf->length());
  ted->show_insert_position();
  inp->value("");
  inp->take_focus();
}

int main(int argc, char **argv) {
  Fl_Window win(320, 240, "BIC");
  Fl_Text_Buffer txtbuf;
  win.begin();
  Fl_Text_Display ted(2, 2, 320 - 2, 240 - 2 - 32 - 2);
  Fl_Input inp(2, 240 - 2 - 32, 320 - 2, 32);
  win.end();

  inp.callback(input_cb, (void *)&ted); // set callback
  inp.when(FL_WHEN_ENTER_KEY);          // when ENTER is pressed

  ted.buffer(txtbuf);
  txtbuf.append("first line\n");
  win.resizable(ted);
  win.show();
  return Fl::run();
}

There's a little more code involved to position to the end of the buffer to display new text when entered and to set the input focus back to the input widget, but I tried to make the example complete. There's no error handling though.