NOGDUS $1670.00 has been donated to NOGDUS!
May 28, 2017, 08:55:25 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Getting started with ClanLib - Part IV  (Read 3698 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: March 14, 2009, 11:22:17 AM »

Getting started with ClanLib - Part IV

In this article I am going to show you how the work with the keyboard.
Whats that? I already did that? Well sort of. What I did before was ok to get started, but its more useful to have our input handled separately from the main method. That is what we are doing now.

Go duplicate the previous code-base and make your edits like I told you to before. ...I'll wait.....

Done? Ok. Lets continue. Cheesy

We are going to add a couple more private methods to our class:
Code:
/// handles key press events
void KeyDownEventHandler(const CL_InputEvent& event);

/// handles key release events
void KeyUpEventHandler(const CL_InputEvent& event);


Now, after we get the graphics context, we will grab the input context of the window:
Code:
// get the input context of the window
CL_InputContext* inputContext = mainWindow.get_ic();

Lets get the keyboard device next:
Code:
// get the keyboard device
CL_InputDevice keyboardDevice = inputContext->get_keyboard();

Now, we attach our keyboard event handlers:
Code:
// attach our signal handlers for the keyboard input events
CL_Slot windowKeyDownEventSlot = keyboardDevice.sig_key_down().connect(this, &CLTest4Application::KeyDownEventHandler);
CL_Slot windowKeyUpEventSlot = keyboardDevice.sig_key_down().connect(this, &CLTest4Application::KeyUpEventHandler);

remove this code from the main loop since we are handling input via signals now:
Code:
// if the ESC key is pressed then
if (CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
{
// stop the main loop
running_ = false;
}

Now we get our event handler method for when a key is pressed:
Code:
void CLTest4Application::KeyDownEventHandler(const CL_InputEvent& event)
{
switch(event.id)
{
case CL_KEY_ESCAPE:
{
// stop the main loop
running_ = false;
} break;

default: break;
}
}

And the event handler method for when a key is released:
Code:
void CLTest4Application::KeyUpEventHandler(const CL_InputEvent& event)
{
switch(event.id)
{
default: break;
}
}

You are right. it doesn't do anything...yet. It just exists now to be complete. Grin

We're done!
Build and run and you should see nothing different from the Test3 executable..but we know better. Cool
Stay tuned for the next article!


Logged

Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!