FLTK logo

Re: [fltk.general] fltk/test/sudoku - more keyboard support

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: fltk/test/sudoku - more keyboard support Albrecht Schlosser Jun 01, 2021  
 
Hi Mark, please don't top-post here, see
https://en.wikipedia.org/wiki/Posting_style#Top-posting

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/8437564b-efa0-3a39-d895-e6ff398e2ec4%40online.de.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.