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 » LineEdit, EditFields, DocEdit » How to capture Key Press events?
How to capture Key Press events? [message #38119] Sun, 02 December 2012 02:03 Go to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
How to do something when the user types on a EditString?
Re: How to capture Key Press events? [message #38121 is a reply to message #38119] Sun, 02 December 2012 07:38 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1758
Registered: August 2008
Location: Czech Republic
Senior Contributor

lectus wrote on Sun, 02 December 2012 02:03

How to do something when the user types on a EditString?

There is WhenAction callback just for this Wink
struct App : TopWindow {
	typedef App CLASSNAME;
	EditString s;
	Label l;
	App(){
		Add(s.HSizePosZ(5,5).TopPosZ(5,25));
		Add(l.HSizePosZ(5,5).TopPosZ(35,25));
		s.WhenAction << THISBACK(DoSomething);
	}
	void DoSomething(){
		l.SetLabel("Length: "+IntStr(AsString(~s).GetCharCount()));
	}
};


Best regards,
Honza
Re: How to capture Key Press events? [message #38125 is a reply to message #38119] Sun, 02 December 2012 13:44 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
I see. Thanks!

So, the WhenAction callback is mostly a generic callback, right? Depends on the type of Ctrl.

In the case of a EditField (or derivate) it will get called when the user presses a key.
Re: How to capture Key Press events? [message #38126 is a reply to message #38125] Sun, 02 December 2012 14:13 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
If an EditField has fucus and key/s are pressed (or on release) then a function can be called. Or even a mouse over or hover can be used in some instances like highlighting.
Here are a few where the Enter or Return key is pressed.
Any (almost) key or combination can be used.
It is generic as in many languages it is fairly similar.

	bool Key(dword key, int count){
//	bool MyApp::Key(dword key, int count){
    	if (key == K_RETURN && Point1.HasFocus()) {
            Point1Action();
	        return true;
    	}
    	if (key == K_RETURN && Point2.HasFocus()) {
            Point2Action();
	        return true;
    	}
    	if (key == K_RETURN && Distance.HasFocus()) {
            DistanceAction();
	        return true;
    	}
    	if (key == K_RETURN && Angle1.HasFocus()) {
            Angle1Action();
	        return true;
    	}
    	if (key == K_RETURN && Angle2.HasFocus()) {
		    Angle2<<="OK a2";
	        return true;
    	}
    	if (key == K_RETURN && timezone.HasFocus()) {
			String TZ;
			TZ = ~timezone;
    		tz = (int)atof(TZ);
//    		tz = (int)atof(~timezone);
	        return true;
    	}
	}

edit: I have two buttons that when clicked it is the same as pushing the Enter/Return key. It's handy on a tablet so the on screen keyboard does not have to be pulled up.

[Updated on: Sun, 02 December 2012 14:35]

Report message to a moderator

Re: How to capture Key Press events? [message #38127 is a reply to message #38121] Sun, 02 December 2012 14:42 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
dolik.rce wrote on Sun, 02 December 2012 01:38

There is WhenAction callback just for this Wink



This is very handy feature. I used this in a EditString so when the user types something the code will reload the arrayctrl filtering with SQL whatever the user typed.

U++ produces great user experience in applications. Smile
Re: How to capture Key Press events? [message #38129 is a reply to message #38125] Sun, 02 December 2012 14:56 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1758
Registered: August 2008
Location: Czech Republic
Senior Contributor

lectus wrote on Sun, 02 December 2012 13:44

So, the WhenAction callback is mostly a generic callback, right? Depends on the type of Ctrl.

Yes, it is very generic. In some Ctrls it might even never be called Smile The general idea is that it is called whenever the "value" of the widget changes. However given how generic it is, it can do pretty much anything, depending on what the author of the widget deems useful, so it is always better to check the docs or code to see when you can expect it to be executed and when not.

What nlneilson mentioned is another way to achieve this by inheritance and method overloading. It can be used too, should be bit more general (you can use it even for Ctrls where WhenAction callback usage is not/badly implemented), but it is most probably overkill in this case Wink

Quote:

U++ produces great user experience in applications.
That's one of the main goals, IIRC Very Happy

Honza
Re: How to capture Key Press events? [message #38413 is a reply to message #38129] Fri, 14 December 2012 14:55 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12124
Registered: November 2005
Ultimate Member
dolik.rce wrote on Sun, 02 December 2012 08:56

The general idea is that it is called whenever the "value" of the widget changes.


By USER ACTION.

It is fundamental to know that assigning value to widget does NOT trigger WhenAction.
Re: How to capture Key Press events? [message #38425 is a reply to message #38413] Sat, 15 December 2012 09:39 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1758
Registered: August 2008
Location: Czech Republic
Senior Contributor

mirek wrote on Fri, 14 December 2012 14:55

dolik.rce wrote on Sun, 02 December 2012 08:56

The general idea is that it is called whenever the "value" of the widget changes.


By USER ACTION.

It is fundamental to know that assigning value to widget does NOT trigger WhenAction.


Oh, right... I completely forgot to mention that Embarassed Sorry.
Previous Topic: need some expert advice to extend the LineEdit
Next Topic: BUG: LineEdit "Drag And Drop" violates read-only state
Goto Forum:
  


Current Time: Fri Dec 13 15:10:35 CET 2019

Total time taken to generate the page: 0.01680 seconds