|
|
Home » U++ Library support » LineEdit, EditFields, DocEdit » How to capture Key Press events?
|
Re: How to capture Key Press events? [message #38121 is a reply to message #38119] |
Sun, 02 December 2012 07:38 |
|
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
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 #38126 is a reply to message #38125] |
Sun, 02 December 2012 14:13 |
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 #38129 is a reply to message #38125] |
Sun, 02 December 2012 14:56 |
|
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 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
Quote: | U++ produces great user experience in applications.
| That's one of the main goals, IIRC
Honza
|
|
|
|
Re: How to capture Key Press events? [message #38425 is a reply to message #38413] |
Sat, 15 December 2012 09:39 |
|
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 Sorry.
|
|
|
Goto Forum:
Current Time: Mon May 06 05:30:51 CEST 2024
Total time taken to generate the page: 0.02535 seconds
|
|
|