Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
zeiler is currently offline  zeiler
Messages: 2
Registered: November 2010
Junior Member
Hello,

I'm new to the forum (and the UPP-Framework). I really like both Smile 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:
MyButton 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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Julian,
welcome here Wink

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
Re: Push button by keystroke (without ALT) [message #29924 is a reply to message #29923] Tue, 30 November 2010 11:47 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3356
Registered: August 2008
Senior Veteran
Or this?

class MyButton : public Button {
private:
	virtual void LeftDown(Point p, dword key) {
		Button::LeftDown(p, key);		
		if (key & K_ALT)
			PromptOK("Done");	
	}
};


Best regards
IƱaki
Re: Push button by keystroke (without ALT) [message #29967 is a reply to message #29923] Wed, 01 December 2010 17:55 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
dolik.rce wrote on Tue, 30 November 2010 05:31

Hi Julian,
welcome here Wink

You can trigger the button action from a Key() handler of your application. Have a look at its description in Ctrl reference.



There is even the nice method "PseudoPush" that makes push animation... Smile
Re: Push button by keystroke (without ALT) [message #30061 is a reply to message #29920] Mon, 06 December 2010 09:05 Go to previous message
zeiler is currently offline  zeiler
Messages: 2
Registered: November 2010
Junior Member
Thank you all for your help and sorry for replying so late.

Julian
Previous Topic: How to center the top most window?
Next Topic: printing RichEditWithToolBar rotated 90 degrees?
Goto Forum:
  


Current Time: Fri Apr 19 01:40:57 CEST 2024

Total time taken to generate the page: 0.02498 seconds