Page 1 of 1

Clicking a PushButton using the enter key

Posted: Thu Nov 25, 2010 13:45
by BigG
Hi,

I came across the following problem: I want to fire the onClicked() event of a CEGUI::PushButton when the button has the input focus and the enter/return key is pressed.
Surprisingly, I was unable to find any topic related to this in here, so I came up a few solutions:

1) The one requiring fewer changes to the library, but requiring more user-code: adding a public procedure click() to CEGUI::PushButton:
in CEGUIPushButton.h

Code: Select all

virtual void click(void);

in CEGUIPushButton.cpp

Code: Select all

void PushButton::click(void)
{
    WindowEventArgs args(this);
    onClicked(args);
}

Using this procedure I could add some OnKeyDown/OnKeyUp handler to every button in my interface that I want to be enter-clickable.

2) The one requiring the more changes to the library, but almost no user code: adding a boolean property "EnterClickable" to CEGUI::PushButton:
I didn't write the required code for that yet, but it should be a pretty trivial task, which I could fulfill if required :)
The idea is simple: adding a boolean property "EnterClickable" to the PushButton class. Also, it would require an additional event handler in the PushButton class that handles keyboard input, checks if the clicked key is the enter key and if "EnterClickable" is true, and if so, fires the onClicked event.


Maybe a combination of both would be handy in other cases as well - like adding a click() procedure usable by the user but also the "EnterClickable" property so that the most common use of the click() procedure would be handled build-in and also configurable in the layout files and such.

I'll be happy about any feedback on this ;)

Greetings, BigG

Re: Clicking a PushButton using the enter key

Posted: Mon Nov 29, 2010 22:19
by Jamarr
I am sure you can achieve this without modifying any CEGUI source code by sub-classing CEGUI::PushButton, subscribing to KeyDown/KeyUp, and firing the clicked event as necessary.