FLTK logo

[fltk.general] Losing keyboard input under user 'spamming' input

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 ]

Losing keyboard input under user 'spamming' input rs Sep 23, 2021  
 
Hi,

I have a modal window that I want to be toggled shown/hidden using some keyboard shortcut (lets say alt+S).

The way I am doing this is setting the shortcut to show the window in a menu item and then, since the shown window grabs focus away from the main window, using a  subclassed  FLTK window for the toggled window such that its handle causes it to hide itself when the user uses the same shortcut (i.e. Alt+s).

Under sensible user input and on certain OS's this all works fine. However on Linux, and unlike on Mac (I haven't checked Windows yet), holding buttons down seems to generate multiple key FL_KEYBOARD events (as opposed to just a single one when it is depressed/released).

As such the user can simply hold Alt + S, effectively spamming the shortcut. After, say, a second of this something breaks and the whole FLTK program simply doesn't appear to receive keyboard events anymore (other events like mouse presses are fine). I thought it might have been that focus got "lost" somewhere and so tried refocusing on the main window, but this didn't work.

I have other instances where holding down a button can be dealt with just fine (where it is just updating some data, not hiding/showing/setting FLTK objects), so it is something to do with the hiding and showing of the window - maybe some increasing queue of window hide/show calls are overflowing some buffer due to them accumulating faster than the OS can actually hide and show them, I don't know.

Anyway here's a (bodged) minimal example that replicates the issue on Linux (FLTK v.1.4.x)

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Menu_Bar.H>
#include <iostream>


int shown_v;


class my_win : public Fl_Double_Window{
    
    int handle(int);
public:
    my_win(int x, int y,int w,int h,const char* e):Fl_Double_Window(x,y,w,h,e){};
};

int my_win::handle (int e){
    int ret = Fl_Double_Window::handle(e);
    if (e==FL_KEYBOARD){
        if (Fl::event_state()&(FL_ALT))  {
            if (Fl::event_key()=='s'){
                if (shown_v){
                    this->hide();
                    shown_v=0;
                }
                else{
                    this->show();
                    shown_v=1;
                }
                return 1;
            }
        }
    }
    return ret;
}

void toggle_win(Fl_Widget*,void* data){
    my_win* w = static_cast<my_win*>(data);

    if (shown_v){
        w->hide();
        shown_v=0;
    }
    else{
        w->show();
        shown_v=1;
    }
}

int main() {

    shown_v=0;

    my_win *win2 = new my_win(0.5*Fl::w()-100,0.5*Fl::h()-100,200,200,"Second win");
    win2->end();
    
    Fl_Double_Window *win = new Fl_Double_Window(0.5*Fl::w()-200,0.5*Fl::h()-200,400,400,"Main Win");
     

    Fl_Menu_Item items[] = {
        {"Menu1",0,0,0,FL_SUBMENU,0,0,FL_NORMAL_SIZE,FL_BLACK}, //0
        {"Hide/show second window",FL_ALT+'s',toggle_win,win2,0,0,0,FL_NORMAL_SIZE,FL_BLACK},
        {0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0}
    };

    Fl_Menu_Bar *menu = new Fl_Menu_Bar(0, 0, win->w(),25);
    menu->menu(items);
    win->end();
    win->show();
    
    
    return Fl::run();

}


Thanks,

R.

--
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/eac6d692-a200-4051-8fc1-e01e56b88cden%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'.