|
keyListener
Adding a key listener to see if the user is typing any keys is
very easy. It is very similar to mouseListener
In our class decleration we need to implements
KeyListener
Under init():
addKeyListener(this);
Now we need to declare three keylistener statements:
public void keyPressed(KeyEvent e) {
}
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:
/*
* Test class to practice mouse movement by drawing a circle where the cursor is.
*
* @author Jeff Borland
* @date 12-8-10
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Keys extends JApplet implements MouseListener, MouseMotionListener,ActionListener,KeyListener
{
JButton testButton=new JButton("test");
JTextArea testArea=new JTextArea(5,10);
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setFocusable(true);
Container con = getContentPane();
con.setBackground(Color.white);
con.setLayout (new FlowLayout() );
con.add(testButton);
con.add(testArea);
testArea.setText("Type a letter\n");
testArea.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue));
testButton.addActionListener(this);
}
public void paint (Graphics g)
{
super.paint(g);
}
//This is the method that will be called if a key is pressed
//It is neccessary if you are using getKeyCode()
public void keyPressed(KeyEvent e)
{
char theChar=e.getKeyChar();
int theCode=e.getKeyCode();
testArea.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
//It is unneccessary to use this method, but you must include it
public void keyReleased(KeyEvent e)
{
}
//This is the method that will be called if a key is typed, however you cant use getKeyCode, so
// you are better to use keyPressed
public void keyTyped(KeyEvent e)
{
}
//when someone presses the mouse button
public void mousePressed(MouseEvent e)
{
}
//when someone releases the mouse button
public void mouseReleased(MouseEvent e)
{
}
// when the mouse enters the applet
public void mouseEntered(MouseEvent e)
{
}
//when the mouse leaves the applet
public void mouseExited(MouseEvent e)
{
}
//when the mouse button is clicked
public void mouseClicked(MouseEvent e)
{
requestFocus(); //this bring the focus make to the main progra
}
//the mouse button is pressed and the mouse makes a significantly large movement
public void mouseDragged(MouseEvent e)
{
}
//the mouse makes a significantly large movement
public void mouseMoved(MouseEvent e)
{
}
public void actionPerformed(ActionEvent thisEvent)
{
Object source = thisEvent.getSource();
if (source == testButton)
{
testArea.setText("Starting Over:");
}
//now have if statements seeing finding out where the action occured
}
}
Assignments:
- Create an applet IsArrow that will have a textfield that will
say arrow or not arrow when they hit a key.
- Create an applet hideImage that when the space bar is pressed,
an image
will show or hid on the screen.
|