Game Development Reference
In-Depth Information
The following code snippet shows how we would detect whether the number 3 key
has just been pressed:
if ((s3eKeyboardGetState(s3eKey3) & S3E_KEY_STATE_PRESSED) != 0)
{
// Number 3 key has just been pressed!
}
Detecting key state changes using callbacks
It is also possible to be informed whenever a key is pressed or released by using a
callback function. Callbacks are preferred by many coders since they force us into
writing smaller, more manageable functions that often yield a more concise and
reusable solution. The polled approach to key detection may seem easier at first
glance but it is easy to end up with a codebase that has key state checking logic
spread across many source files. Using the callback approach will tend to ensure
key handling code is implemented in a more structured way.
To set up a callback function that detects key state changes, we use the
s3eKeyboardRegister function. We provide this function with the enumeration
value S3E_KEYBOARD_KEY_EVENT to identify the type of callback we are setting up,
a pointer to a function that will be the callback, and a void pointer that can be used
to pass in our own custom data to the callback function.
When a key is pressed or released, the function we specified will be called. The
callback function is passed a pointer to an s3eKeyboardEvent structure, which
details the key press or release and is also provided with the custom data pointer we
specified when registering the callback.
When we no longer wish to receive key state notifications, we can call
s3eKeyboardUnRegister to disable the callback mechanism. We just need
to pass the S3E_KEYBOARD_KEY_EVENT enumeration and the pointer to our
callback method to stop the callbacks from occurring any more.
Here's a code snippet to illustrate how we might detect state changes to the
number 3 key:
// Callback function that will receive key state notifications
int32 KeyStateCallback(s3eKeyboardEvent* apKeyEvent,
void* apUserData)
{
if (apKeyEvent->m_Key == s3eKey3)
{
if (apKeyEvent->m_Pressed)
{
 
Search WWH ::




Custom Search