PDA

View Full Version : Two questions regarding user input (callback functions)



haimat
09-30-2012, 05:21 PM
Hi all, I have two questions regarding user input:

1.)
My main program class "Program" contains a static method

static void keyboardInput(const CL_InputEvent &event, const CL_InputState &state);
In the main method I then do the following:

CL_InputDevice keyboard = window.get_ic().get_keyboard();
keyboard.sig_key_down().connect(&Program::keyboardInput);
And the method itself looks like this:

void Program::keyboardInput(const CL_InputEvent &event, const CL_InputState &state)
{
if(event.id == CL_KEY_ESCAPE)
shutdown = true;
}
Unfortunately this does not work, the static method is never called when a key is pressed.
What do I wrong here?

2.)
By now I poll mouse button clicks (no callback as above, that works). I want to exit the app when the user clicks the window's X (i.e. close) button in the top-right corner -- as any other window as well under MS Windows. However, when I click on the X button at my ClanLib window, nothing happens. How can I achieve to close the app when the user clicks on X?

Thanks in advance!

Kind regards, Matthias

sphair
09-30-2012, 07:15 PM
Whenever hooking up a CL_Signal, you need to keep the CL_Slot around:

slot_input = keyboard.sig_key_down().connect(&Program::keyboardInput);

(and define CL_Slot slot_input somewhere it doesn't go out of scope).

Read more at http://clanlib.org/wiki/MainDocs:Template_Callbacks


If you want to close the window when the user presses the X, you need to hook up CL_DisplayWindow sig_window_close:

Example:
CL_Slot slot_quit = window.sig_window_close().connect(this, &MyClass::on_window_close);

Check out almost any example in the Examples directory for similar stuff.

haimat
09-30-2012, 09:17 PM
thank you!