Game Development Reference
There are a few places in your game where you could add this code, but it might make the most sense
to add it in the _da_gC]iaKran method, outside of the eb statement.
Entering numbers with the Enter key
At the moment, you can enter numbers by clicking the Guess button. Wouldn't it be nice if you could
also enter them by hitting the Enter key? Of course it would!
Capturing key presses will be discussed in detail in the next chapter, but here's a sneak peek for the
adventurous among you.
It isn't too difficult, but there's one major modification you have to make to the structure of your
code. If you click the Guess button or press the Enter key, you'll want the same directives to run, so
the Enter key and Guess button both need to call the same method. Create a new method called
c]iaArajpo and have it contain all the directives that are currently in the kjCqaoo>qppkj?he_g event
handler. Then call the c]iaArajpo method from both kjCqaoo>qppkj?he_g and the new event handler
you create for your Guess button.
Up for a challenge? Try this modification and see whether you can make it work. (You can find the
complete final source code for this modification in the chapter's source files in a folder called Jqi^an
1. To capture key presses with your program, you need to import the Gau^k]n`Arajp and Gau^k]n`
classes into your program. Add the following two eilknp directives in bold to the top of your
2. Add an event listener to the op]ca object that listens for key presses. Add the following code
in bold text to the ejep method: