Results 1 to 2 of 2

Thread: input in an applet

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    input in an applet

    hey hey its final project time in my java class

    im making a simple game (kind of like a 1 player slimeball), but Im not sure how to get keyboard input in an applet. I know how to get it in normal console, but not too sure about applets. Thats all I need, thanks all!

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
     import java.awt.event.*;
    
     class B extends KeyAdapter{
         
       public void keyPressed(KeyEvent ke){
           System.out.println(ke.getKeyChar());       
       }
     }
    Here is a short description of the KeyEvent class.

    "This event is generated when the user presses or releases a key, or does bolth (ie types a character) These situations are characterized by the following constants in the KeyEvent class"

    public static int KEY_PRESSED
    public static int KEY_RELEASED
    public static int KEY_TYPED

    The last one is what you want.

    "This event is delivered when a character has been typed on the keyboard ie.... a key has been pressed (KEY_PRESSED) and then released (KEY_RELEASED) to signify typing a character.

    There are two methods that are defined in the KeyEvent class.....

    int getkeyCode()

    For KEY_PRESSED or KEY_RELEASED events, this method can be used to get the integer key-code associated with the key. The key codes are defined as constants in the KeyEvent.

    char getKeyChar()

    For KEY_TYPED events, the method can be used to get the Unicode character theat results from hitting a key.

    If you want you can also (which i think is cool) log the time the user pressed the key. By using the getWhen() method inherited from the parent class InputEvent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width