OK,
here is tho code greg proposed. I made couple changes, but htey don't affect 
this input behavior. The only change in the code is at the place where you 
check for the key value. I found that the key value at the numberical keyboard 
is 65456 for 0 and 65465 for 9 (65454 is for .). With this little change the 
code allows the input and display it well in the widget, but when I send the 
cmd as a arguement of some function I recieve some wrong characters instead of 
the numbers(±˛ł´µ¶·¸ą - this is the output of 123456789). I am using 
VS2008 on XP.
Thanks, Tomas

#include <string.h>
#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Text_Editor.H>
//
// Simple terminal simulator (PROOF OF CONCEPT) -- 1.00 erco 07/30/2009
//
class MyTerminal : public Fl_Text_Editor {
    Fl_Text_Buffer *buff;
    int in, out;
    char cmd[1024];
public:
    MyTerminal(int X,int Y,int W,int H,const char* L=0) : 
Fl_Text_Editor(X,Y,W,H,L) {
        buff = new Fl_Text_Buffer();
        buffer(buff);
        textfont(FL_COURIER);
        textsize(12);
        in = out = 0;
        cmd[0] = 0;
    }
    // Append to buffer, keep cursor at end
    void append(const char*s) {
        buff->append(s);
        // Go to end of line
        insert_position(buffer()->length());
        scroll(count_lines(0, buffer()->length(), 1), 0);
    }
    int handle(int e) {
        switch (e) {
            case FL_KEYUP: {
                int key = Fl::event_key();
                if ( key == FL_Enter ) break;
//***CHANGE***
                if ( key != FL_BackSpace && (key < ' ' || key > 0x7f)&& (key < 
65454 || key > 65465) ) return(0);
                if ( key == FL_BackSpace && cmd[0] == 0 ) return(0);
                break;
            }
            case FL_KEYDOWN: {
                int key = Fl::event_key();
                // Enter key? Execute the command, clear command buffer
                if ( key == FL_Enter ) {
                    fprintf(stderr, "EXECUTING: '%s'\n", cmd); // <-- EXECUTE 
YOUR COMMANDS HERE
                    cmd[0] = 0;
                    break;
                }
//***CHANGE***
                if ( key != FL_BackSpace && (key < ' ' || key > 0x7f ) && (key 
< 65454 || key > 65465) ) return(0);
                if ( key == FL_BackSpace ) {
                    // Remove a character from end of command buffer
                    if ( cmd[0] ) {
                        cmd[strlen(cmd)-1] = 0;
                        break;
                    } else {
                        return(0);
                    }
                } else {
                    if ( strlen(cmd) > sizeof(cmd)-2 ) return(0);
                    // Append text to our 'command' buffer
                    int i = strlen(cmd);
                    cmd[i+0] = (char)key;
                    cmd[i+1] = 0;
                }
                break;
            }
        }
        return(Fl_Text_Editor::handle(e));
    }
};
int main() {
    Fl_Double_Window win(620,520,"Terminal Test");
    MyTerminal edit(10,10,win.w()-20,win.h()-20);
    edit.append("Line one\nLine Two\nHere is your prompt: ");
    win.resizable(win);
    win.show();
    return(Fl::run());
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to