andrei_c wrote:
> 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:

> int Canvas::handle(int event) // handle the event
> {
>   switch(event)
>   {

This must be FL_KEYBOARD, and you must call Fl::event_key() to get the 
key value.

>     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:

[...]

> 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.

There's no need to write a function that handles only key events, the 
first version would do it as well, but there are some problems.

There is no event named Fl_Right. You must handle FL_KEYBOARD events and 
use Fl::event_key() to get the key value (the second version does this 
correctly).

To get key events your widget must return 1 on FL_PUSH and FL_FOCUS 
events. I don't think that Fl_Box::handle() would do this, so you'd 
better add this in your handle() method. Then you must click once in 
your canvas, and your widget should get key events.

For a start you could try to set Fl::focus(my_canvas) before calling 
Fl::run().

Another trick would be to handle FL_SHORTCUT events. They are basically 
the same as FL_KEYBOARD events, but they are delivered to all widgets, 
if no widget handles the FL_KEYBOARD event. However, I wouldn't 
recommend this for real applications.

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

Reply via email to