[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Inputs from the keyboard in Java

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1569958573252{background-color: #f6f6f6 !important;}”]

The class KeyAdapter in Java makes it possible to take inputs from the keyboard and use in your programs

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

How to get inputs from the keyboard in Java?

In our game we want to allow the user to operate the game using the keyboard.

 

  • A common way to accomplish this is to use Java’s built-in KeyAdapter class. It is a class used to receive input from the keyboard.
  • Further, we will also use the built-in KeyEvent class. When a key is pressed, released, or typed, the relevant method activates in the listener object, and KeyEvent transferres then to it.
  • Finally, we then create a new class of our own, KeyInput, which will handle all button presses in the game.

[/vc_column_text][vc_empty_space height=”30px”][vc_separator type=”normal”][vc_empty_space height=”15px”][vc_column_text]

How to use Keyadapter in Java?

 

Start by importing the classes:

 

import java.awt.event.KeyAdapter;              
import java.awt.event.KeyEvent;

 

Then we inherit the KeyAdapter class which allows you to take inputs from the keyboard (Remember inheritance?)

 

public class KeyInput extends KeyAdapter { 

   Game game;

 

Additionally, we create the constructor that takes the  game object as instance variable

 

public KeyInput(Game game){                
    this.game = game;
}

 

Further, what will happen when a key is pressed? We attache pressing the button to the object called game

 

public void keyPressed(KeyEvent e){        
    game.keyPressed(e);                    
}

 

Finally, what happens when a key is released? We attach that command to the object called game

 

    public void keyReleased(KeyEvent e){       
        game.keyReleased(e);                   
    }
}

 

The complete set of code is:

 

import java.awt.event.KeyAdapter;              
import java.awt.event.KeyEvent;                

public class KeyInput extends KeyAdapter {     

        Game game;

    public KeyInput(Game game){                
        this.game = game;
    }

    public void keyPressed(KeyEvent e){        
        game.keyPressed(e);                    
    }

    public void keyReleased(KeyEvent e){       
        game.keyReleased(e);                   
    }
}

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”15px”][vc_column_text]

How use KeyEvent in Java?

We created our KeyInput class above that contains two methods, one for when we press a key and one for when we release a key. But now we want to make sure that the program actually detects which key is pressed.

 

Further, we will use the built-in methods VK_LEFT and VK_RIGHT found in KeyEvent to detect if it is the Left and Right arrow keys used.[/vc_column_text][vc_empty_space height=”40px”][vc_single_image image=”20081″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][vc_empty_space height=”40px”][vc_column_text]Let’s start by declaring a method for detecting if a key is pressed

 

public void keyPressed(KeyEvent e){                                          

    // New key press
    int key = e.getKeyCode();                                                

    // If we press right
    if (key == KeyEvent.VK_RIGHT) {                                          
        System.out.println("The right arrow key is pressed");

    // Or, if we press left
    } else if (key == KeyEvent.VK_LEFT) {                                    
        System.out.println("The left arrow key is pressed");
    }
}

 

Additionally, what will happen when a key is released

 

public void keyReleased(KeyEvent e){                                         

    // New key has been released
    int key = e.getKeyCode();                                                

    // If we release the right key
    if(key == KeyEvent.VK_RIGHT){                                            
        System.out.println("The right arrow key is released");
    }

    // If we release the left key
    else if(key == KeyEvent.VK_LEFT){                                        
        System.out.println("The left arrow key is released");
    }
}

 

 

Note, we can of course do the same for other keys. The Oracle KeyEvent page lists commands for the other keys

 

Finally, the complete set of code is:

 

public void keyPressed(KeyEvent e){                                          

    // New key press
    int key = e.getKeyCode();                                                

    // If we press right
    if (key == KeyEvent.VK_RIGHT) {                                          
        System.out.println("The right arrow key is pressed");

    // Or, if we press left
    } else if (key == KeyEvent.VK_LEFT) {                                    
        System.out.println("The left arrow key is pressed");
    }
}

public void keyReleased(KeyEvent e){                                         

    // New key has been released
    int key = e.getKeyCode();                                                

    // If we release the right key
    if(key == KeyEvent.VK_RIGHT){                                            
        System.out.println("The right arrow key is released");
    }

    // If we release the left key
    else if(key == KeyEvent.VK_LEFT){                                        
        System.out.println("The left arrow key is released");
    }
}

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”40px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]