|
|
Thank you very much. My final version of the function is: ```c++ int Sudoku::handle(int event) { if (event == FL_SHORTCUT) { int j, k; if (Fl::event_key(FL_Home)) { find_focus_cell(&j, &k); grid_cells_[j][0]->take_focus(); return 1; } else if (Fl::event_key(FL_End)) { find_focus_cell(&j, &k); grid_cells_[j][8]->take_focus(); return 1; } else if (Fl::event_key(FL_Page_Down)) { find_focus_cell(&j, &k); grid_cells_[8][k]->take_focus(); return 1; } else if (Fl::event_key(FL_Page_Up)) { find_focus_cell(&j, &k); grid_cells_[0][k]->take_focus(); return 1; } else if (*Fl::event_text() == 'm') { // 'm' for middle grid_cells_[4][4]->take_focus(); return 1; } } return Fl_Double_Window::handle(event); }
```
On Tuesday, June 1, 2021 at 11:28:45 AM UTC+1 Albrecht Schlosser wrote:
On 6/1/21 10:36 AM 'Mark' via fltk.general wrote:
I trie adding these methods:
```
int Sudoku::handle(int event) {
if (event == FL_KEYBOARD) {
// ...
}
return Fl_Widget::handle(event);
}
```
The result is that the _only_ keyboard events that are
recognized are Home, End, PageUp, PageDown: none of the normal
arrow key and numbers etc., nor the menus work anymore!
You're pretty close, but ...
Point 1: you must call Fl_[Double_]Window::handle(event) rather than
Fl_Widget::handle(event). Your handle() method short-circuited all
the "magic" of Fl_Group's event delivery. You should [almost] always
use the handle() method of the direct parent (base) class. In this
case I'd *guess* that using Fl_Group::handle(event) would work as
well. I assume that most of your problems are caused by this fault.
Also my `find_focus_cell()` function never works; it always
leaves the values at `-1`. This turns out not to be a surprise
because FL_SHORTCUT only happens if Fl::focus() returns nullptr.
However, to avoid this I use FL_KEYBOARD. But it still doesn't
work.
Point 2: what you really need is the FL_SHORTCUT event. FL_KEYBOARD
events are sent directly to the Fl::focus() widget which is in the
sudoko program always a cell. If the focus widget doesn't use the
event (returns 0) it is converted to FL_SHORTCUT and sent to (a lot
of!) other widgets. At some point it will arrive at the Sudoku class
which is why I suggested to add a handle() method to the Sudoku
class.
That all said, I used your code with minimal changes (and I also
added take_focus() statements) and it works as I expect:
int Sudoku::handle(int event) { if (event == FL_SHORTCUT) { // needs FL_SHORTCUT int j, k; if (Fl::event_key(FL_Home)) { find_focus_cell(&j, &k); printf("home %d %d\n", j, k); grid_cells_[j][0]->take_focus(); // ADDED return 1; } else if (Fl::event_key(FL_End)) { find_focus_cell(&j, &k); printf("end %d %d\n", j, k); grid_cells_[j][8]->take_focus(); // ADDED return 1; } else if (Fl::event_key(FL_Page_Down)) { find_focus_cell(&j, &k); printf("pg down %d %d\n", j, k); grid_cells_[8][k]->take_focus(); // ADDED return 1; } else if (Fl::event_key(FL_Page_Up)) { find_focus_cell(&j, &k); printf("pg up %d %d\n", j, k); grid_cells_[0][k]->take_focus(); // ADDED return 1; } } return Fl_Double_Window::handle(event); // needs Fl_[Double_]Window:: }
So clearly I have a long way to go understanding how FLTK
event-handling works!
It's not a simple model but if you read the docs (maybe not only
once!) you'll get a feeling how it works step by step. Although in
some cases it may be surprising and looking at the FLTK code and the
comments may be helpful.
Besides reading the docs I suggest to take a look at
test/handle_events.cxx which shows a way to print events in a better
readable form. You can also modify it to find out what you need to
know in practice.
--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/81cb67b8-e891-4a06-a8a0-5abf0a63be63n%40googlegroups.com.
[ Direct Link to Message ] | |
|
| |