Login    New User    Forgot Password    Help      

Keyboard Test

Java Tutorials

Example

// keyboard test

/*
<applet code="ev1" width =300 height = 200 >
</applet>
*/

/*

Class Variable Represented Key

Event.HOME The Home key
Event.END The End key
Event.PGUP The Page Up key
Event.PGDN The Page Down key
Event.UP The up arrow
Event.DOWN The down arrow
Event.LEFT The left arrow
Event.RIGHT The right arrow
Event.f1 The f1 key
Event.f2 The f2 key
Event.f3 The f3 key
Event.f4 The f4 key
Event.f5 The f5 key
Event.f6 The f6 key
Event.f7 The f7 key
Event.f8 The f8 key
Event.f9 The f9 key
Event.f10 The f10 key
Event.f11 The f11 key
Event.f12 The f12 key

*/

 

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ev4 extends Applet implements KeyListener
{

String msg="";
int cox=10,coy=20; // output cordinates

public void init()
{
setBackground(Color.white);
addKeyListener(this);
requestFocus(); // request inpur focus
}

public void keyPressed(KeyEvent ke)
{
showStatus("Key Down ");
int key=ke.getKeyCode();

switch(key)
{
case KeyEvent.VK_F1:
msg+="<F1>";
break;

case KeyEvent.VK_F2:
msg+="<F2>";
break;

case KeyEvent.VK_F3:
msg+="<F3>";
break;

case KeyEvent.VK_PAGE_DOWN:
msg+="<pgdown>";
break;

case KeyEvent.VK_PAGE_UP:
msg+="<up>";
break;

case KeyEvent.VK_LEFT:
msg+="<left>";
break;

case KeyEvent.VK_RIGHT:
msg+="<Right>";
break;

case KeyEvent.VK_UP:
msg+="<left>";
break;

case KeyEvent.VK_DOWN:
msg+="<Right>";
break;

case KeyEvent.VK_ENTER:
msg+="<Enter>";
break;

}

repaint();

}

public void keyReleased(KeyEvent ke)
{
showStatus( "Key up ");
}

public void keyTyped(KeyEvent ke)
{
msg+=ke.getKeyChar();
repaint();
}

public void paint(Graphics g)
{
g.drawString(msg,cox,coy);
}

}

Output

Output in applet

Java Tutorials