Оглавление |
Назад
// ==========================================
// KeyInfo.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
// =======================================
// Class KeyInfo
// =======================================
public class KeyInfo
{
// =======================================
// main
// =======================================
public static void main(String args[])
{
FrameWindow frame;
frame = new FrameWindow("Keyboard monitor");
frame.init();
frame.show();
}
}
// =======================================
// Class FrameWindow
// =======================================
class FrameWindow extends Frame
implements KeyListener, WindowListener
{
int yHeight;
Dimension wndDimension;
// ============================================
// FrameWindow
// ============================================
public FrameWindow(String szTitle)
{
super(szTitle);
setSize(400, 400);
setBackground(Color.yellow);
setForeground(Color.black);
}
// ============================================
// init
// ============================================
public void init()
{
this.addKeyListener(this);
this.addWindowListener(this);
}
// ============================================
// keyPressed
// ============================================
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_F3)
{
setVisible(false);
System.exit(0);
}
showKeyboardInfo(e, true);
lineFeed();
}
// ============================================
// keyReleased
// ============================================
public void keyReleased(KeyEvent e)
{
showKeyboardInfo(e, false);
lineFeed();
}
// ============================================
// keyTyped
// ============================================
public void keyTyped(KeyEvent e)
{
}
// ============================================
// showKeyboardInfo
// ============================================
void showKeyboardInfo(KeyEvent e, boolean down)
{
Graphics g = getGraphics();
g.setFont(new Font("Courier",
Font.PLAIN, 12));
String s = "";
s = "Code: " + e.getKeyCode() +
", " + e.getKeyText(e.getKeyCode());
if(down)
s += " (press)";
else
s += " (release)";
g.drawString(s, 10, 50);
}
// ============================================
// lineFeed
// ============================================
void lineFeed()
{
Graphics g = getGraphics();
g.setFont(new Font("Courier",
Font.PLAIN, 12));
FontMetrics fm = g.getFontMetrics();
yHeight = fm.getHeight();
wndDimension = getSize();
g.copyArea(0, 1,
wndDimension.width - 1,
wndDimension.height - yHeight - 5,
0, yHeight + 1);
g.setColor(Color.yellow);
g.fillRect(1, 1,
wndDimension.width - 2,
yHeight + 1);
}
// ============================================
// paint
// ============================================
public void paint(Graphics g)
{
super.paint(g);
}
// ============================================
// windowClosing
// ============================================
public void windowClosing(WindowEvent e)
{
setVisible(false);
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
|