How to set a Default Button?
by Markus Nuebel · in Torque Game Engine · 08/29/2004 (10:14 am) · 14 replies
I have a small gui with an OK and a Cancel button.
Like in windows I want to make the OK button the default button, that get's pressed, when the user presses ENTER.
How do I enable a button to be a default button?
-- Markus
Like in windows I want to make the OK button the default button, that get's pressed, when the user presses ENTER.
How do I enable a button to be a default button?
-- Markus
About the author
#2
I set "enter" as the accelerator but still nothing happens, when I hit enter, while the dialog with the button is active.
Is there anything else I need to set, to enable this?
Thanks.
Markus
08/30/2004 (10:20 pm)
Drew,I set "enter" as the accelerator but still nothing happens, when I hit enter, while the dialog with the button is active.
Is there anything else I need to set, to enable this?
Thanks.
Markus
#3
I tried some other options and I couldn't figure this one out. The text boxes like to absorb the enter command. Maybe someone else can enlighten us, or maybe the gui controls need some modification on the engine side to allow this to happen.
08/31/2004 (7:48 am)
I just tried this in a couple of places. It looks like it works as long as the dialog doesn't have edit boxes on it. I have one dialog that is just a few buttons and it works fine.I tried some other options and I couldn't figure this one out. The text boxes like to absorb the enter command. Maybe someone else can enlighten us, or maybe the gui controls need some modification on the engine side to allow this to happen.
#4
this adds a "wantReturn" boolean, which determines whether the edit box will catch the return key of whether it will pass the return key to the parent to process. In your case, you want the enter accelerator to trigger, so wantReturn should be set to false for every edit box in the dialog. I've set it to default to false, because I assume that most edit boxes do not actually need to get the enter key.
in guiTextEditCtrl.h
change this:
into this:
in guiTextEditCtrl.cc
change this:
to this:
change this:
to this:
lastly, add
to the constructor.
This change WILL BREAK any edit box that wants a return, so you will have to go and manually check "wantReturn" for every edit control that is expecting to process the enter key. In most cases though, you should be able to leave it alone.
I do not think this breaks the editor gui, althogh i haven't had a chance to test it yet.
EDIT: formatting, spelling, bugs, etc
08/31/2004 (1:50 pm)
Ok, so here's a simple fix to make the enter accelerator work even when a guiTextEditCtrl has focus.this adds a "wantReturn" boolean, which determines whether the edit box will catch the return key of whether it will pass the return key to the parent to process. In your case, you want the enter accelerator to trigger, so wantReturn should be set to false for every edit box in the dialog. I've set it to default to false, because I assume that most edit boxes do not actually need to get the enter key.
in guiTextEditCtrl.h
change this:
bool mInsertOn; S32 mMouseDragStart; Point2I mTextOffset; bool mDragHit; bool mTabComplete; S32 mScrollDir;
into this:
bool mInsertOn; S32 mMouseDragStart; Point2I mTextOffset; bool mDragHit; bool mTabComplete; [b]bool mWantReturn;[/b] S32 mScrollDir;
in guiTextEditCtrl.cc
change this:
addField("tabComplete", TypeBool, Offset(mTabComplete, GuiTextEditCtrl));
addField("deniedSound", TypeAudioProfilePtr, Offset(mDeniedSound, GuiTextEditCtrl));to this:
addField("tabComplete", TypeBool, Offset(mTabComplete, GuiTextEditCtrl));
[b]addField("wantReturn", TypeBool, Offset(mWantReturn, GuiTextEditCtrl));[/b]
addField("deniedSound", TypeAudioProfilePtr, Offset(mDeniedSound, GuiTextEditCtrl));change this:
case KEY_RETURN:
case KEY_NUMPADENTER:
//first validate
onLoseFirstResponder();
mHistoryDirty = false;to this:
case KEY_RETURN:
case KEY_NUMPADENTER:
[b]if(!mWantReturn && !mSinkAllKeyEvents)
return false;[/b]
//first validate
onLoseFirstResponder();
mHistoryDirty = false;lastly, add
mWantReturn = false;
to the constructor.
This change WILL BREAK any edit box that wants a return, so you will have to go and manually check "wantReturn" for every edit control that is expecting to process the enter key. In most cases though, you should be able to leave it alone.
I do not think this breaks the editor gui, althogh i haven't had a chance to test it yet.
EDIT: formatting, spelling, bugs, etc
#5
09/04/2004 (1:43 pm)
Hey, that's pretty snazzy - on my list of potential engine fixes.
#6
That's because there is no "Enter" key. yes it says "Enter" on your keyboard, but the platform responds to that key as "return".
In other words...
accelerator = "return"; - Works perfectly
accelerator = "enter"; - Wont work at all
11/14/2004 (9:52 pm)
Quote:I set "enter" as the accelerator but still nothing happens, when I hit enter, while the dialog with the button is active.
That's because there is no "Enter" key. yes it says "Enter" on your keyboard, but the platform responds to that key as "return".
In other words...
accelerator = "return"; - Works perfectly
accelerator = "enter"; - Wont work at all
#7
I took a guess and put it inside "guiTextEditCtrl.cc" around line 21 after
GuiTextEditCtrl::GuiTextEditCtrl()
{
It works but wanted to clarify. Thanks for posting... just what i was looking for.
04/28/2006 (9:59 am)
Where is the constructor? Is it in "guiTextEditCtrl.cc"?? Or do you mean in "defaultGameProfiles.cs"? I took a guess and put it inside "guiTextEditCtrl.cc" around line 21 after
GuiTextEditCtrl::GuiTextEditCtrl()
{
It works but wanted to clarify. Thanks for posting... just what i was looking for.
#8
08/15/2007 (9:07 am)
Is there any other way to get accelerators to work in this situation without code changes?
#9
10/13/2007 (3:26 pm)
I can't seem to get it to work myself. I have asked the great guys over at Mydreamrpg.com in the IRC chat to see if they have any ideas.
#10
05/17/2008 (9:16 pm)
Sheesh this is such an old post but it helped me out. Thanks.
#11
Change needed in chathud.gui
new GuiTextEditCtrl(MessageHud_Edit)
{
profile = "ChatHudEditProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "0 13";
extent = "10 22";
minExtent = "8 8";
visible = "1";
altCommand = "$ThisControl.eval();";
escapeCommand = "MessageHud_Edit.onEscape();";
historySize = "5";
maxLength = "120";
// This was added so sending messages still works
wantreturn = true;
};
06/06/2008 (11:58 pm)
Helped me too! One thing though, if you're using the stock starter.fps, when you go to send a message the text edit box there actually WANTS the enter key, so you need to set the wantreturn to true for this edit box. This drove me nuts for a few days figuring out what was going on as I had made other changes since implementing this and hadn't yet noticed this was happening until way later. Change needed in chathud.gui
new GuiTextEditCtrl(MessageHud_Edit)
{
profile = "ChatHudEditProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "0 13";
extent = "10 22";
minExtent = "8 8";
visible = "1";
altCommand = "$ThisControl.eval();";
escapeCommand = "MessageHud_Edit.onEscape();";
historySize = "5";
maxLength = "120";
// This was added so sending messages still works
wantreturn = true;
};
#12
Set the default value in constructor to "true" and whenever you want to use this behavior, set "wantreturn" to "false" in script. This is the default behavior that GG assumed. Thus, you will not brake any already existing textEditCtrl.
10/21/2008 (3:59 am)
I have a strong recommendation for (backward) compatibility:Set the default value in constructor to "true" and whenever you want to use this behavior, set "wantreturn" to "false" in script. This is the default behavior that GG assumed. Thus, you will not brake any already existing textEditCtrl.
#13
We can add a callback function in guiTextEditCtrl to receive enter key event and give an interface to script, so that we can do anything we want in script.
It works in TGB, I don't think that there will by any different in TGE.
In engine source code "guiTextEditCtrl.cc", go to onKeyDown function where we receive all key events. What we are interested in is just about KEY_RETURN and KEY_NUMPADENTER, which means pressing enter key.
So, just go to the case:
and at the end of the case:
Insert the very simple code:
So finally it looks like:
And in script, when you add some guiTextEditCtrl named as "GuiTxtEditChat", you can have this function:
When you hit enter key in guiTextEditCtrl, this function will be called, so you can do anything you want here.
Have fun, guys!
12/10/2008 (10:40 pm)
I think I've a better idea that changing less to make this work.We can add a callback function in guiTextEditCtrl to receive enter key event and give an interface to script, so that we can do anything we want in script.
It works in TGB, I don't think that there will by any different in TGE.
In engine source code "guiTextEditCtrl.cc", go to onKeyDown function where we receive all key events. What we are interested in is just about KEY_RETURN and KEY_NUMPADENTER, which means pressing enter key.
So, just go to the case:
case KEY_RETURN:
case KEY_NUMPADENTER:and at the end of the case:
return true;
Insert the very simple code:
Con::executef(this, 1, "onReturnEx");
So finally it looks like:
if (mProfile->mReturnTab)
{
GuiCanvas *root = getRoot();
if (root)
{
root->tabNext();
return true;
}
}
Con::executef(this, 1, "onReturnEx");
return true;And in script, when you add some guiTextEditCtrl named as "GuiTxtEditChat", you can have this function:
function GuiTxtEditChat::onReturnEx(%this)
{
echo("Hello onReturnEx");
}When you hit enter key in guiTextEditCtrl, this function will be called, so you can do anything you want here.
Have fun, guys!
#14
myOkayButton.performClick();
This will cause your OK button to be virtually clicked in script, assuming your button is named "myOkayButton".
04/22/2009 (1:01 pm)
No code changes are required to do this because a callback is already designated for the "return" event... the control objects that like to grab the "return" events, like guiTextEditCtrl and guiTextListCtrl, automatically call the "AltCommand" (if specified) when <Enter> is pressed. So in the GuiEditor, select the offending control and give it an "AltCommand" like:myOkayButton.performClick();
This will cause your OK button to be virtually clicked in script, assuming your button is named "myOkayButton".
Torque Owner Drew Hitchcock
You'll probably also want to set the accelerator field for the cancel button to "escape"