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 "'Mark' via fltk.general" Jun 01, 2021  
 
I trie adding these methods:
```
int Sudoku::handle(int event) {
    if (event == FL_KEYBOARD) {
    int j, k;
    if (Fl::event_key(FL_Home)) {
        find_focus_cell(&j, &k);
        printf("home %d %d\n", j, k);
        return 1;
    }
    else if (Fl::event_key(FL_End)) {
        find_focus_cell(&j, &k);
        printf("end %d %d\n", j, k);
        return 1;
    }
    else if (Fl::event_key(FL_Page_Down)) {
        find_focus_cell(&j, &k);
        printf("pg down %d %d\n", j, k);
        return 1;
    }
    else if (Fl::event_key(FL_Page_Up)) {
        find_focus_cell(&j, &k);
        printf("pg up %d %d\n", j, k);
        return 1;
    }
    }
    return Fl_Widget::handle(event);
}

void Sudoku::find_focus_cell(int *x, int *y) {
    *x = *y = -1;
    auto focus_widget = Fl::focus();
    if (focus_widget != nullptr) {
    int j, k;
    for (j = 0; j < 9; ++j) {
        for (k = 0; k < 9; ++k) {
        if (grid_cells_[j][k] == focus_widget) {
            *x = j;
            *y = k;
            return;
        }
        }
    }
    }
}
```

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!

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.

So clearly I have a long way to go understanding how FLTK event-handling works!

On Monday, May 31, 2021 at 4:43:06 PM UTC+1 Albrecht Schlosser wrote:
On 5/31/21 10:15 AM 'Mark' via fltk.general wrote:
Tucked away in fltk's test directory is a rather nice sudoku game.
I've been trying to do some modifications. The main one being:
```
  // Show N cells...
  count = (11 * (5 - difficulty_)) - 1;
```
By adding `- 1` the Hard level defaults to 32 filled in cells which appears to be the standard (cf https://en.wikipedia.org/wiki/Sudoku ). Before it was 33.

Hmm, nice to know, but not a hard restriction, is it? The article says "no more than 32" which is still not true for the "Medium" and "Easy" levels.


I also changed the font from Helvetica Bold to Helvetica Roman.

Just a matter of taste, I think?


But the changes I'd really like to make are to add keyboard support for: Home, End, PgUp, PgDown to move the focus to the left-most cell in the current row, right-most cell in the current row, top-most cell in the current column, and bottom-most cell in the current column.

Could anyone give me some help with doing this please?

Interesting, this looks a little more difficult.

I think the main task is to set the Fl::focus() widget to an arbitrary (calculated) cell, depending on the keystroke, i.e. in class Sudoku this would be something like

  grid_cells_[i][j].take_focus();

The question is how to find the index i and j and how (in what context) to execute the take_focus() statement. Unfortunately the cells don't "know" their own indices, but that's probably not necessary.

One possible approach would be to find the index by testing which cell has the focus (Fl::focus()) and then calculate the new cell index. This could be done in a handle() method added to the Sudoku class. Pseudo code:

  int Sudoku::handle(int e) {
    switch(e) {
      case FL_KEYDOWN:
      case FL_SHORTCUT:
        int i, j;
        // 1) find the cell with focus -> i, j
        // 2) calculate new cell index depending on key value
       
grid_cells_[i][j].take_focus();
        return 1;
      default:
        break;
    }
    return Fl_Window::handle(e);
  }


This is only one potential way which might work (untested) with only a few code changes. I'm sure there are different ways...

I hope this can give you a start at least. Have fun!

--
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/4a7a3b54-c756-4f27-9404-b7b43528cccbn%40googlegroups.com.
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'.