|
From: algodon c. <alg...@gm...> - 2009-03-26 14:08:20
|
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) 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. Thanks in advance -- http://www.myspace.com/algodonciego http://www.myspace.com/1h2f |
|
From: LukenShiro <luk...@ng...> - 2009-03-26 15:10:51
|
giovedì 26 marzo 2009, algodon ciego ha scritto: > Hello ! > I want keyboard shortcuts to trigger checkbuttons. How do i do that ? [..] > Unfortunately it only works when i use the mouse, not when i use the > keyboard. AFAIR, in order to have a working shortcut you must operate on a focused window, and you maybe need to press ALT+yourkey as explained in http://pyfltk.sourceforge.net/docs/fltk.html#Fl_Button BTW it's unnecessary to repeat key reference two times (as a label and as a .shortcut) -- GNU/Linux * Slackware 'current' GNU/Linux * Bluewhite64 'current' LU #210970 LM #98222 SU #12583 |
|
From: Robert A. <ro...@gm...> - 2009-04-02 18:47:50
|
On Thu, Mar 26, 2009 at 7:08 AM, algodon ciego <alg...@gm...> 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
|