The Mouse and Keyboard classes

Frontend::Utils contains the two classes Mouse and Keyboard (new in Beta3, Frontend::Input no longer handles mouse and keyboard). These classes can be used to handle input events from one or more windows easily. In this tutorial, we will use them to handle the input from our single game window created by SimpleSetup.

Classes like Mouse and Keyboard should be initialized before we enter the mainloop (see previous part), but after SimpleSetup::Start() is called.

Mouse mouse(setup.GetWindow());
Keyboard keyboard(setup.GetWindow());

That’s it! Mouse and Keyboard now hook the appropriate events from our game window and provides some nice functions for us:

  • Keyboard::KeyDown() - Checks if a particular keyboard key is held down
  • Keyboard::KeyTrigger() - Checks if a particular keyboard key was pressed down since last call to this function. This is useful if you want something to happen only once per key press.
  • Mouse::ButtonDown() - Checks if a particular mouse button is held down
  • Mouse::ButtonTrigger() - Checks if a particular mouse button was pressed down since last call to this function. This is useful if you want something to happen only once per button press.

As an example, we will now use the space bar to control the clear color of the window we set up in the previous part. Replace the code inside your mainloop with this:

if (keyboard.KeyDown(GUI::KeySpace))
{
    setup.GetGraphicsDevice()->Clear(Graphics::ClearBuffersAll, 0, 1, 0, 0, 1, 0);
}
else
{
   setup.GetGraphicsDevice()->Clear(Graphics::ClearBuffersAll, 1, 0, 0, 0, 1, 0);
}

And there you go :) I guess you are able to figure out how to use Keyboard and Mouse now by checking out the documentation :)

Source code
The source code for this tutorial can be found in the subversion repository at http://svn2.assembla.com/svn/fwg/trunk/tutorials/simple/part2/




FireStats icon Powered by FireStats