[收藏]WINFORM(C#)的键盘测试

来源:互联网 发布:大溪地黑珍珠 知乎 编辑:程序博客网 时间:2024/05/15 05:10

using System;
using System.Drawing;
using System.Windows.Forms;
public class KeyExamine: Form
{
 public static void Main()
 {
  Application.Run(new KeyExamine());
 }
 
 enum EventType
 {
  None, KeyDown, KeyUp, KeyPress
 }
 struct KeyEvent
 {
  public EventType evttype;
  public EventArgs evtargs;
 }
 const int iNumLines  = 25;
 int   iNumValid  = 0;
 int   iInsertIndex = 0;
 KeyEvent[] akeyevt = new KeyEvent[iNumLines];
 int xEvent, xChar, xCode, xMods, xData,
  xShift, xCtrl, xAlt, xRight;
 public KeyExamine()
 {
  Text = "Key Examine";
  BackColor = SystemColors.Window;
  ForeColor = SystemColors.WindowText;
  xEvent = 0;
  xChar = xEvent + 5 * Font.Height;
  xCode = xChar + 5 * Font.Height;
  xMods = xCode + 8 * Font.Height;
  xData = xMods   + 8 * Font.Height;
  xShift = xData  + 5 * Font.Height;
  xCtrl = xShift +5 * Font.Height;
  xAlt = xCtrl +5 * Font.Height;
  xRight = xAlt +5 * Font.Height;
  ClientSize = new Size(xRight, Font.Height * (iNumLines + 1));
  FormBorderStyle = FormBorderStyle.Fixed3D;
  MaximizeBox = false;
 }
 protected override void OnKeyDown(KeyEventArgs kea)
 {
  akeyevt[iInsertIndex].evttype = EventType.KeyDown;
  akeyevt[iInsertIndex].evtargs = kea;
  onKey();
 }
 protected override void OnKeyUp(KeyEventArgs kea)
 {
  akeyevt[iInsertIndex].evttype = EventType.KeyUp;
  akeyevt[iInsertIndex].evtargs = kea;
  onKey();
 }
 protected override void OnKeyPress(KeyPressEventArgs kpea)
 {
  akeyevt[iInsertIndex].evttype = EventType.KeyPress;
  akeyevt[iInsertIndex].evtargs = kpea;
  onKey();
 }
 void onKey()
 {
  if(iNumValid < iNumLines)
  {
   Graphics grfx = CreateGraphics();
   DisplayKeyInfo(grfx, iInsertIndex, iInsertIndex);
   grfx.Dispose();
  }
  else
  {
   ScrollLines();
  }
  iInsertIndex = (iInsertIndex + 1) % iNumLines;
  iNumValid = Math.Min(iNumValid + 1, iNumLines);
 }
 protected virtual void ScrollLines()
 {
  Rectangle rect = new Rectangle(0, FontHeight,
   ClientSize.Width,
   ClientSize.Height - Font.Height);
  Invalidate(rect);
 }
 protected override void OnPaint(PaintEventArgs pea)
 {
  Graphics grfx = pea.Graphics;
  BoldUnderLine(grfx, "Event", xEvent, 0);
  BoldUnderLine(grfx, "KeyChar", xChar, 0);
  BoldUnderLine(grfx, "KeyCode", xCode, 0);
  BoldUnderLine(grfx, "Modifiers", xMods, 0);
  BoldUnderLine(grfx, "KeyData", xData, 0);
  BoldUnderLine(grfx, "Shift", xShift, 0);
  BoldUnderLine(grfx, "Control", xCtrl, 0);
  BoldUnderLine(grfx, "Alt", xAlt, 0);

  if(iNumValid < iNumLines)
  {
   for (int i = 0; i < iNumValid; i++)
    DisplayKeyInfo(grfx, i, i);
  }
  else
  {
   for (int i = 0; i < iNumLines; i++)
    DisplayKeyInfo(grfx, i, (iInsertIndex + i) % iNumLines);
  }
 }
 void BoldUnderLine(Graphics grfx, string str, int x, int y)
 {
  Brush brush = new SolidBrush(ForeColor);
  grfx.DrawString(str, Font, brush, y, y);
  grfx.DrawString(str, Font, brush, x+1, y);
  SizeF sizef = grfx.MeasureString(str, Font);
  grfx.DrawLine(new Pen(ForeColor), x, y + sizef.Height,
   x+sizef.Width, y+sizef.Height);
 }
 void DisplayKeyInfo(Graphics grfx, int y, int i)
 {
  Brush br = new SolidBrush(ForeColor);
  y = (1+y) * Font.Height;
  grfx.DrawString(akeyevt[i].evttype.ToString(), Font, br, xEvent, y);
  if (akeyevt[i].evttype == EventType.KeyPress)
  {
   KeyPressEventArgs kpea =
    (KeyPressEventArgs) akeyevt[i].evtargs;
   string str = String.Format("/x202D{0} (0x{1:X4})",
    kpea.KeyChar, (int) kpea.KeyChar);
   grfx.DrawString(str, Font, br, xChar, y);
  }
  else
  {
   KeyEventArgs kea = (KeyEventArgs) akeyevt[i].evtargs;
   string str = String.Format("{0} ({1})", kea.KeyCode, (int) kea.KeyCode);
   grfx.DrawString(str, Font, br, xCode, y);
   grfx.DrawString(kea.Modifiers.ToString(), Font, br, xMods, y);
   grfx.DrawString(kea.KeyData.ToString(), Font, br, xData, y);
   grfx.DrawString(kea.Shift.ToString(), Font, br, xShift, y);
   grfx.DrawString(kea.Control.ToString(), Font, br, xCtrl, y);
   grfx.DrawString(kea.Alt.ToString(), Font, br, xAlt, y);
  }
 }
}