What's the goal: is it to add up/dn/lt/rt key control
        to the caret position of the text display widget?

        If so, I'd think you'd want to derive a class from
        Fl_Text_Display to handle() the key events.

        This, instead of deriving from Fl_Window.

        When a widget has focus, in this case Fl_Text_Display,
        it gets first shot at the events, and it may consume them.
        In fact, it may try to take all keyboard events when it
        has focus, which would probably explain why the Fl_Window
        derived class isn't getting them.


On 05/15/12 06:41, David Currie wrote:
> #include <FL/Fl.H>
> #include <FL/Fl_Window.H>
> #include <FL/Fl_Text_Display.H>
> #define USING_CARET
> class EventWindow : public Fl_Window
> {
> private:
>   Fl_Text_Buffer*  mBuff;
>   Fl_Text_Display* mDisp;
>   bool                   mKeyPressed;
>   unsigned long    mKey;
>   int handle_keydn(int event, int key, int state);
> public:
>   EventWindow();
>   bool getKey(unsigned long& pKey);
>   int handle(int pEvent);
> #ifdef USING_CARET
>   void showCaret(int pPos);
> #endif
> };
> EventWindow::EventWindow() : Fl_Window(640,480,"Display")
> {
>   mBuff = new Fl_Text_Buffer();
>   mDisp = new Fl_Text_Display(20,20,640-40,480-40);
>   mDisp->buffer(mBuff); add(*mDisp);
>   mBuff->text("line 0\nline 1\nline 2\nline 3\nline 4\nline 5\n");
>   mKeyPressed = false;
>   mKey = 0;
> }
> int EventWindow::handle(int pEvent)
> {
>   switch (pEvent) {
>     case FL_FOCUS: case FL_UNFOCUS: case FL_KEYUP:
>       return 1;
>     case FL_KEYBOARD:
>       return handle_keydn(pEvent,Fl::event_key(),Fl::event_state());
>     default:
>       return Fl_Window::handle(pEvent);
>   };
> }
> int EventWindow::handle_keydn(int pEvent, int pKey,int pState)
> {
> printf("KeyDN Key = %d State = %d\n",pKey,pState);fflush(stdout);
>   mKey = (unsigned long)pState + (unsigned long)pKey;
>   mKeyPressed = true;
>   return 1;
> }
> bool EventWindow::getKey(unsigned long& pKey)
> {
>   bool lRet = false;
>   if (mKeyPressed)
>   {
>     switch (mKey)
>     {
>       default:
>       pKey = mKey;
>       lRet = true;
>       case 0xFFE3: // left ctrl
>       case 0xFFE4: // righ ctrl
>       case 0xFFE9: // left alt
>       case 0xFFEA: // left alt
>       case 0xFFE1: // left shift
>       case 0xFFE2: // righ shift
>       break;
>     }
>     mKeyPressed = false;
>     mKey = 0;
>   }
>   return lRet;
> }
> #ifdef USING_CARET
> void EventWindow::showCaret(int pPos)
> {
>   mDisp->show_cursor();  // ENABLE CURSOR
>   mDisp->cursor_color(0xFF000000); // CURSOR RED
>   mDisp->cursor_style(Fl_Text_Display::HEAVY_CURSOR);
>   mDisp->insert_position(pPos); // POSITION
>   mDisp->take_focus(); // SHOW CARET ON STARTUP (EVEN IF NOT IN FOCUS)
> }
> #endif
> int main()
> {
>   EventWindow* win  = new EventWindow();
>   win->show();
>   int lPos = 5;
> #ifdef USING_CARET
>   win->showCaret(lPos);
> #endif
>   int lRet = 0;
>   while (1)
>   {
>     lRet = Fl::wait();
>     unsigned long lKey = 0;
>     if (win->getKey(lKey))
>     {
>       ++lPos;
>       printf(" lPos = %d\n",lPos);fflush(stdout);
> #ifdef USING_CARET
>       win->showCaret(lPos);
> #endif
>     }
>     if (!lRet)
>     {
>       break;
>     }
>   }
>   return Fl::run();
> }

_______________________________________________
fltk-dev mailing list
fltk-dev@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to