email me at borlaj@portlandschools.org
notes previous (09/07/06) submit the dump links  
 

keyListener

 

To listen for keys, there are three methods we could use, most of the time we will just use keyPressed

 

public void keyPressed(KeyEvent e) { }

 

The other two you might want are:

public void keyReleased(KeyEvent e) { }

public void keyTyped(KeyEvent e) { }

All of our code to use keyboard input should be under keyPressed

To access what key it was you can use:

  • getKeyChar() - this returns a letter typed (char)
  • getKeyCode() - this returns an int of what they typed (int). To be used for arrows and non-alphanumeric stuff

The getKeyCode will return a number, they match up with constants listed here. We access them similar to colors that we used before. For example, if we want to detect if the user pressed up.

	    int theCode=e.getKeyCode();
        if (theCode==KeyEvent.VK_UP)           
        {
            //do this   
        }
			

 

In action:

/**
*Text generated by Simple GUI Extension for BlueJ
*Update by Jeff Borland
*
*
**/

import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.border.Border;
import java.awt.image.*;
import java.util.Scanner; 
import javax.swing.*;


/**
*Description of project goes here 
*Author
*
*
**/

public class KeyPractice extends BorlandBase {

	
	private JTextArea textarea1;

	//Constructor 
	public void initialize(){

		setWidth(500);
		setHeight(400);
		setScreenLayout(null);
		setBackgroundColor(new Color(192,192,192));


		textarea1 = new JTextArea();
		textarea1.setBounds(183,13,285,361);
		textarea1.setBackground(new Color(255,255,255));
		textarea1.setForeground(new Color(0,0,0));
		textarea1.setEnabled(true);
		textarea1.setFont(new Font("sansserif",0,12));
		textarea1.setText("Type a letter. \n");
		textarea1.setBorder(BorderFactory.createBevelBorder(1));
		textarea1.setVisible(true);

		//adding components to contentPane panel
		addItem(textarea1);
	}


	//If you would like something painted when program opens 
	//public void paintScreen(Graphics g) 
	{ 

	} 


	//This is the method that will be called if a key is pressed 
	public void keyPressed(KeyEvent e)
	{
		char theChar=e.getKeyChar();
        int theCode=e.getKeyCode();
        //I will now append [means add] that info to the textbox.
        //at the end is say \n which means new line
        textarea1.append("The key typed is " + theChar + " and the code of the key is "+ theCode+ "\n");       

	}

	//This is the method that will be called if a key is released
	public void keyReleased(KeyEvent e)
	{
		//char theChar=e.getKeyChar();
		//int theCode=e.getKeyCode();

	}

	//This is the method that will be called if a key is typed
	public void keyTyped(KeyEvent e) { }
	{
		//char theChar=e.getKeyChar();
		//int theCode=e.getKeyCode();

	}

	 public static void main(String[] args){
		try{  BorlandBase.main((BorlandBase)(new Object() { }.getClass().getEnclosingClass().newInstance()));  }
		catch (Exception e){e.printStackTrace();}
	}

}
		  

 

 

Assignments:

  1. Create an applet IsArrow that will have a textfield that will say arrow or not arrow when they hit a key.
  2. Create an applet hideImage that when the space bar is pressed, an image will show or hid on the screen.
    • use drawImage