Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Push button by keystroke (without ALT)
Push button by keystroke (without ALT) [message #29920] |
Tue, 30 November 2010 09:49 |
zeiler
Messages: 2 Registered: November 2010
|
Junior Member |
|
|
Hello,
I'm new to the forum (and the UPP-Framework). I really like both Nice work, the people are friendly, the answers helpful. I already searched the forum for my problem but didn't find the exact problem I have. Now here's my problem: I need the ability to push a button or executing the corresponding button callback when a key is pressed (without the ALT key).
My current solution is to derive a class from Upp::Button calling it MyButton and adding a function to that class which returns the protected Button-variable accesskey. Then I overwrite the main Windows Key()-function and add call button.PseudoPush when the access key is pressed.
Well, maybe I'll better show you the code:
In the header file I have the following:
class MyButton : public Button {
public:
dword GetAccessKey();
};
dword MyButton::GetAccessKey()
{
return ToLower((wchar)accesskey);
}
In the main Windows class declaration I add the button:
In the main Windows constructor:
button.SetLabel("E&xit");
And finally in the overwritten Key()-function of the main Window:
bool MyMainWindow::Key(dword key, int count)
{
if ( button.GetAccessKey() == key )
{
button.PseudoPush();
return true;
}
return TopWindow::Key(key, count);
}
Now here's my question:
Is there a better, cleaner, more elegant solution to this problem?
Best regards,
Julian
|
|
|
Re: Push button by keystroke (without ALT) [message #29923 is a reply to message #29920] |
Tue, 30 November 2010 11:31 |
|
Hi Julian,
welcome here
You can trigger the button action from a Key() handler of your application. Have a look at its description in Ctrl reference.
A simple example would be:#include <CtrlLib/CtrlLib.h>
using namespace Upp;
class Win: public TopWindow{
typedef Win CLASSNAME;
public:
Button b;
Win(){
Add(b.HCenterPos().VCenterPos());
b.SetLabel("Close");
b<<=THISBACK(OnPush);
}
void OnPush(){
Exclamation("Button pushed, exiting...");
Close();
}
bool Key(dword key,int){
if(key=='q'){
b.Action();
return true;
}
return false;
}
};
GUI_APP_MAIN{
Win().Run();
}
Another possible solution could use InstallKeyHook() which works for entire app, not only one window. It is documented in the same document.
Best regards,
Honza
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Apr 25 14:04:22 CEST 2024
Total time taken to generate the page: 0.03422 seconds
|