Game Development Reference
In-Depth Information
else
{
OnKeyUp(this, new KeyEventArgs(keys));
}
}
}
}
The keyboard state treats each key as a button with a Pressed and Held
member. A subclass called KeyState contains the state of each key on the
keyboard. The Keyboard constructor takes in a reference to the OpenGL con-
trol and adds delegates to its KeyUp and KeyDown events. These events are then
used to update the state of the entire keyboard. The KeyPress event is also
given a delegate, and this in turn fires another event called KeyPressEvent ,
passing on the data. KeyPressEvent is used when the user is typing the player
name or entering data. When using the keyboard as a gaming device, the keys can
be treated as buttons and queried with the functions IsKeyPressed and
IsKeyHeld .
The slightly complicated part of the keyboard class is the polling of keys. This
requires a C function to be imported from User32.dll and the using Sys-
tem.Runtime.InteropServices; needs to be added to the top of the file.
The KeyUp and KeyDown events aren't fired for the arrow keys so the state of
these keys is determined by GetAsyncKeyState . The PollKeyPress func-
tion uses GetAsyncKeyState to return true if the key is pressed and false if it
isn't. Each frame the arrow keys and the Alt key are polled and the state is
updated.
Create a new test state to confirm that the keyboard works. There's no code
listing for this test state as it's very similar to the mouse state. Test out some of
the keys and the arrow keys to confirm everything is working nicely. Once you
are satisfied—that ends the modifications to the engine. The next step now is to
create a game!
 
Search WWH ::




Custom Search