Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
65 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 The Game
 Threads
 Keyboard and Mouse
 Double Buffering
 Coding the Game

 Printable version
 Discuss this article
 in the forums


The Series
 The Basics
 Making a
 Simple Game

 The Power of Arrays

Keyboard

The next topic we need to look at is how to get the users input from the keyboard. It is actually very easy to check out input from the keyboard because java provides us with two new methods: keyDown and keyUp.

One important point that I should mention before we continue is that before an applet will receive keyboard events (ie. call the keyDown or keyUp methods) the user will need to click on the applet area in order to get the focus. The applet only receives keyboard events when it has the focus.

The following is the syntax for the keyDown method. The keyUp method is indentical except of course the method name is changed to keyUp from keyDown.

  public boolean keyDown(Event e, int key)
  {
    message = "value = " + key;

    return true;
  }

You will notice that the keyDown method takes an integer parameter. This integer value contains the value that represents the key which was pressed. I would recommend that you set up a method similar to the one that I have outlined above and then in your paint method you can print out message (a string) so that you can see the values that are associated with each key.

We now have the capability of accepting input from the keyboard and I think that you will agree with me that it is fairly straight forward.

Mouse

Now that we are up to speed on how to get input from the keyboard we can turn our attention to the mouse. The mouse is just as easy as the keyboard to use, but we have more methods at our disposal when we deal with the mouse.

The following are the methods that we can use for the mouse:

  public boolean mouseEnter(Event e, int x, int y)
  public boolean mouseExit(Event e, int x, int y)
  public boolean mouseDown(Event e, int x, int y)
  public boolean mouseUp(Event e, int x, int y)
  public boolean mouseMove(Event e, int x, int y)
  public boolean mouseDrag(Event e, int x, int y)

One important point to note here is that for each of the above methods you can simply return true.

All of the methods for the mouse are fairly clear and understandable. You will notice that each method receives the x and y coordinates of the mouse location with respect to the upper lefthand corner of the applet window.

The mouseEnter and mouseExit methods are called automatically when the mouse enters or exits the applet area. The mouseDown and mouseUp methods are called when a mouse button is pressed or released. The mouseMove method is called when the mouse is moved in the applet area. The final method, mouseDrag, is called when the mouse is moved in the applet area while a mouse button is pressed.

The best way to test out these different mouse methods is to set up a basic applet that prints a string out to the screen. The string should be changed by the various mouse methods to reflect which method was called and the current x, y location of the mouse.

You should now have a fairly good idea about what it takes to work with input from the keyboard and mouse in java applets. It is now time to go back into the realm of output.





Next : Double Buffering