Game Development Reference
In-Depth Information
The code in the preceding figure demonstrates how we can capture events to change the
the title of the window every time the
Space
key is pressed and released. Apart from that,
when the
Escape
key is released, the window closes. Notice that
Event::key
contains a
member called
code
, which is an enum of type
Keyboard::Key
. You can use this for-
mula to handle the rest of the event types without much difficulty. However, the case with
Event::EventType::TextEntered
is a bit more interesting. A single key press/
release can be detected and handled in a relatively straightforward manner. When it comes
to certain specific characters though, things start to get a bit more complex. For example,
if we want to detect when the
!
symbol has been typed, we have to look up whether two
individual keys have been pressed at the same time (
Shift
+
1
on most keyboard layouts).
In such cases, SFML saves us a lot of work by providing the simple and easy-to-use
TextEntered
event. The event is only fired when a combination of keys representing
a character are pressed; meaning that a single key (only
Shift
, for example) might not trig-
ger the event. Of course, if
K
alone is pressed, the event will be fired normally, and will
contain the character.