Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы CharInfo.java

Оглавление

Назад

// ==========================================
// CharInfo.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 CharInfo
// =======================================
public class CharInfo
{
  // =======================================
  // 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);
    }
  }

  // ============================================
  // keyReleased
  // ============================================
  public void keyReleased(KeyEvent e)
  {
  }

  // ============================================
  // keyTyped
  // ============================================
  public void keyTyped(KeyEvent e)
  {
    showKeyboardInfo(e);
    lineFeed();
  }

  // ============================================
  // showKeyboardInfo
  // ============================================
  void showKeyboardInfo(KeyEvent e)
  {
    Graphics g = getGraphics();
    g.setFont(new Font("Courier",
      Font.PLAIN, 12));
    
    String s = "";
    
    s = "Char: " + e.getKeyChar() + 
      ", mod = " + e.getModifiers() + 
      " (" + e.getKeyModifiersText(
      e.getModifiers()) + ")";
    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) {}
}

[Назад]