Hello, I'm trying to make a program that uses some keyboard events, so I came
up whit a program that moves a rectangle to the right when the right arrow key
is pressed.But it does not work.
Here's the first version of the program:
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.h>
int XC = 50; // coordonates of the center of the rectangle
int YC = 50;
class Canvas : public Fl_Box // deriving from Fl_Box
{
public:
Canvas( int x, int y, int w, int h, const char *l); // constructor
~Canvas() {}; // destructor
void draw(); // overriding draw()
int handle(int event); // overriding event()
};
Canvas::Canvas( int x, int y, int w, int h, const char *l) : Fl_Box( x, y, w,
h, l)
{
box(FL_FLAT_BOX);
color((Fl_Color)255);
}
void Canvas::draw()
{
Fl_Box::draw();
fl_push_clip( x(), y(), w(), h() );
fl_color(FL_BLACK);
fl_rect( x(), y(), w(), h() ); // draws a contour
fl_rect(x() + XC, y() + YC,50,20); // draws the actual rectangle
fl_pop_clip();
}
int Canvas::handle(int event) // handle the event
{
switch(event)
{
case FL_Right:
XC = XC + 1;
YC = YC + 1;
redraw();
return 1;
default:
return Fl_Box::handle(event);
}
}
/**************************************************************************/
int main()
{
Fl_Double_Window *my_win = new Fl_Double_Window(300,300, "Draw animation");
my_win->begin();
Canvas my_canvas(20,20,260,260,0);
my_win->resizable(my_canvas);
my_win->end();
my_win->show();
return Fl::run();
}
/**************************************************************************/
When I pres the right arrow key it does nothing.
But reading some tutorials I came up whit another version:
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.h>
int XC = 50;
int YC = 50;
class Canvas : public Fl_Box // deriving from Fl_Box
{
public:
Canvas( int x, int y, int w, int h, const char *l); // constructor
~Canvas() {}; // destructor
void draw(); // overriding draw()
int handle(int event); // overriding event()
int handle_key(int event,int key); // to handle only keyboard events
};
Canvas::Canvas( int x, int y, int w, int h, const char *l) : Fl_Box( x, y, w,
h, l)
{
box(FL_FLAT_BOX);
color((Fl_Color)255);
}
void Canvas::draw()
{
Fl_Box::draw();
fl_push_clip( x(), y(), w(), h() );
fl_color(FL_BLACK);
fl_rect( x(), y(), w(), h() ); // draws a contour
fl_rect(x() + XC, y() + YC,50,20); // draws the actual rectangle
fl_pop_clip();
}
int Canvas::handle(int event) // handle the event
{
switch(event)
{
case FL_KEYBOARD:
return handle_key(event,Fl::event_key());
default:
return Fl_Box::handle(event);
}
}
int Canvas::handle_key(int event,int key)
{
switch(key)
{
case FL_Right:
XC = XC + 1;
redraw();
return 1;
default:
damage(1);
return 1;
}
}
/**************************************************************************/
int main()
{
Fl_Double_Window *my_win = new Fl_Double_Window(300,300, "Draw animation");
my_win->begin();
Canvas my_canvas(20,20,260,260,0);
my_win->resizable(my_canvas);
my_win->end();
my_win->show();
return Fl::run();
}
/**************************************************************************/
This doesn't work either.
One question: should I use only the handle() overrided function to handle all
events, including keyboard, or should I do it like the second version??Whit a
special function for keyboard.
Please help me if you have the time, I know I am close, but still not quite
there.
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk