cancel event on FL_Text_Editor

7 views
Skip to first unread message

danielc...@gmail.com

unread,
Mar 12, 2021, 5:55:28 PM3/12/21
to fltk.general
Hi, is it possible to add a cancel event on FL_Text_Editor?
I want on enter key press to do a method instead a new line.
I know how to capture enter key press, I just want to cancel the new line.

Thanks

Greg Ercolano

unread,
Mar 12, 2021, 8:22:51 PM3/12/21
to fltkg...@googlegroups.com

    To prevent the Enter key from inserting a new line into the editor,
    just return from your subclass's handle() method when the FL_ENTER
    key goes up and down.

    That will block the event from ever being seen by the Fl_Text_Editor,
    preventing it from being 'typed'.

    And of course you can call your method instead when that happens.

    Just be sure the event doesn't get to the text editor, both the "key up"
    and the "key down" events.

    So basically (code not tested, may contain typos, an exercise for the reader to solve):



void YourSubclass::handle(int e) {
    if ( e == FL_KEYUP || e == FL_KEYDOWN )
        if ( Fl::event_key() == FL_Enter || FL_KP_Enter ) {
            // Enter key was hit or released
            // Do what you want here, but make sure you DONT
            // let the event be seen by Fl_Text_Editor::handle().
            return 1;        // return, indicating the event was handled by you
        }
    }
    // ..possibly other logic here..
    return Fl_Text_Editor::handle(e);  // let all other events be handle()ed by the text editor
}

Greg Ercolano

unread,
Mar 12, 2021, 8:25:16 PM3/12/21
to fltkg...@googlegroups.com
    Oops, in the example code I posted, this fix for sure is required:

BEFORE:    void YourSubclass::handle(int e) {
AFTER:     int YourSubclass::handle(int e) {
 

Reply all
Reply to author
Forward
0 new messages