On Thu, Mar 26, 2009 at 7:08 AM, algodon ciego <[email protected]> wrote:
> Hello !
> I want keyboard shortcuts to trigger checkbuttons. How do i do that ?
> Here's my code :
>
> self.tb1 = Fl_Check_Button(10,20,60,30,"&A")
> self.tb1.shortcut(97)

this looks better
self.tb1.shortcut('a')

> self.tb1.callback(self.play_cb)
>
> def play_cb(self,ptr):
> print ptr.value()
>
> Unfortunately it only works when i use the mouse, not when i use the
> keyboard.
>


this actually seems like a bug. I tried it in pyfltk and also fltk
c++. Both behave the same, no callback for the shortcut. So it's not a
pyfltk issue.

the defualt when() for the callback to occur for a button is
FL_WHEN_RELEASE. But it's the same when() for the check_button.

here are the possible values (look for Callback "When" Conditions)
http://www.fltk.org/doc-1.1/enumerations.html#Enumerations

However, the docs clearly state :
http://www.fltk.org/doc-1.1/Fl_Check_Button.html
"Buttons generate callbacks when they are clicked by the user. You
control exactly when and how by changing the values for type() and
when()."

I don't think this is totally correct ( ie: to use FL_WHEN_RELEASE by
default for a Check_Button) but it is easy to fix. Just insert this
line

self.tb1.when(FL_WHEN_CHANGED)

here are the int values of when() if you need them
cout<<FL_WHEN_NEVER<<endl; //0
cout<<FL_WHEN_CHANGED <<endl;//1
cout<<FL_WHEN_NOT_CHANGED<<endl;//2
cout<<FL_WHEN_RELEASE<<endl;//4
cout<<FL_WHEN_ENTER_KEY<<endl;//8
cout<<FL_WHEN_RELEASE_ALWAYS<<endl;//6
cout<<FL_WHEN_ENTER_KEY_ALWAYS<<endl;//10

Here is the C++ code I used to test this out

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Check_Button.H>
#include <iostream>
using namespace std;

//---------------------------------------------------

class SimpleWindow : public Fl_Window{

   public:
      SimpleWindow(int w, int h, const char* title );
      ~SimpleWindow();
      Fl_Button* but;
      Fl_Check_Button* checkbut;

   private:

      static void cb_but(Fl_Widget*, void*);
      inline void cb_but_i();

      static void cb_checkbut(Fl_Widget*, void*);
      inline void cb_checkbut_i();

};

//----------------------------------------------------

int main (){

   SimpleWindow win(200,200,"SimpleWindow");
   return Fl::run();
}

//----------------------------------------------------

SimpleWindow::SimpleWindow(int w, int h, const char*
title):Fl_Window(w,h,title){

   begin();


      checkbut = new Fl_Check_Button(20, 100, 70, 30, "a");
      checkbut->callback(cb_checkbut, this);
      checkbut->shortcut('a');
      cout<<"Check_Button when() "<<checkbut->when()<<endl;
      checkbut->when(FL_WHEN_CHANGED); //need this for shortcut callback
        
      but = new Fl_Button(120, 100, 70, 30, "b");
      but->callback(cb_but, this);
      but->shortcut('b');
      cout<<"button when() "<<but->when()<<endl;
        


   end();
   show();
}

//----------------------------------------------------

SimpleWindow::~SimpleWindow(){}

void SimpleWindow::cb_but(Fl_Widget* , void* v) {

   ( (SimpleWindow*)v )->cb_but_i();
}


void SimpleWindow::cb_but_i() {

    cout<<"button callback"<<endl;
}

//----------------------------------------------------

void SimpleWindow::cb_checkbut(Fl_Widget* , void* v) {

   ( (SimpleWindow*)v )->cb_checkbut_i();
}


void SimpleWindow::cb_checkbut_i() {

    cout<<"Check_Button callback"<<endl;
}



-- 
Robert Arkiletian
Eric Hamber Secondary, Vancouver, Canada

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

Reply via email to