home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Control del teclado / KeyExamine / KeyExamine.cs next >
Encoding:
Text File  |  2002-04-24  |  5.8 KB  |  173 lines

  1. //-----------------------------------------
  2. // KeyExamine.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------
  4. using System;    
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. public class KeyExamine: Form 
  9. {
  10.      public static void Main() 
  11.      {
  12.          Application.Run(new KeyExamine());
  13.      }
  14.           // Definiciones enum y struct para guardar eventos de teclas
  15.  
  16.      enum EventType
  17.      {
  18.           None,
  19.           KeyDown,
  20.           KeyUp,
  21.           KeyPress
  22.      }
  23.      struct KeyEvent
  24.      {
  25.           public EventType evttype;
  26.           public EventArgs evtargs;
  27.      }
  28.           // Almacenar eventos de teclas
  29.  
  30.      const int  iNumLines    = 25;
  31.      int        iNumValid    = 0;
  32.      int        iInsertIndex = 0;
  33.      KeyEvent[] akeyevt = new KeyEvent[iNumLines];
  34.  
  35.           // Posicionar el texto
  36.  
  37.      int xEvent, xChar, xCode, xMods, xData, 
  38.          xShift, xCtrl, xAlt, xRight;
  39.      
  40.      public KeyExamine() 
  41.      {
  42.           Text = "Examinar teclas";
  43.           BackColor = SystemColors.Window;
  44.           ForeColor = SystemColors.WindowText;
  45.  
  46.           xEvent = 0;
  47.           xChar  = xEvent + 5 * Font.Height;
  48.           xCode  = xChar  + 5 * Font.Height;
  49.           xMods  = xCode  + 8 * Font.Height;
  50.           xData  = xMods  + 8 * Font.Height;
  51.           xShift = xData  + 8 * Font.Height;
  52.           xCtrl  = xShift + 5 * Font.Height;
  53.           xAlt   = xCtrl  + 5 * Font.Height;
  54.           xRight = xAlt   + 5 * Font.Height;
  55.  
  56.           ClientSize  = new Size(xRight, Font.Height * (iNumLines + 1));
  57.           FormBorderStyle = FormBorderStyle.Fixed3D;
  58.           MaximizeBox = false;
  59.      }
  60.      protected override void OnKeyDown(KeyEventArgs kea)
  61.      {
  62.           akeyevt[iInsertIndex].evttype = EventType.KeyDown;
  63.           akeyevt[iInsertIndex].evtargs = kea;
  64.           OnKey();
  65.      }
  66.      protected override void OnKeyUp(KeyEventArgs kea)
  67.      {
  68.           akeyevt[iInsertIndex].evttype = EventType.KeyUp;
  69.           akeyevt[iInsertIndex].evtargs = kea;
  70.           OnKey();
  71.      }
  72.      protected override void OnKeyPress(KeyPressEventArgs kpea)
  73.      {
  74.           akeyevt[iInsertIndex].evttype = EventType.KeyPress;
  75.           akeyevt[iInsertIndex].evtargs = kpea;
  76.           OnKey();
  77.      }
  78.      void OnKey()
  79.      {
  80.           if(iNumValid < iNumLines)
  81.           {
  82.                Graphics grfx = CreateGraphics();
  83.                DisplayKeyInfo(grfx, iInsertIndex, iInsertIndex);
  84.                grfx.Dispose();
  85.           }
  86.           else
  87.           {
  88.                ScrollLines();
  89.           }
  90.           iInsertIndex = (iInsertIndex + 1) % iNumLines;
  91.           iNumValid = Math.Min(iNumValid + 1, iNumLines);
  92.      }
  93.      protected virtual void ScrollLines()
  94.      {
  95.           Rectangle rect = new Rectangle(0, Font.Height, 
  96.                                          ClientSize.Width,
  97.                                          ClientSize.Height - Font.Height);
  98.  
  99.                // íMe gustaria poder usar desplazamiento!
  100.  
  101.           Invalidate(rect);
  102.      }
  103.      protected override void OnPaint(PaintEventArgs pea)
  104.      {
  105.           Graphics grfx = pea.Graphics;
  106.  
  107.           BoldUnderline(grfx, "Evento",     xEvent, 0);
  108.           BoldUnderline(grfx, "KeyChar",   xChar,  0);
  109.           BoldUnderline(grfx, "KeyCode",   xCode,  0);
  110.           BoldUnderline(grfx, "Modificadores", xMods,  0);
  111.           BoldUnderline(grfx, "KeyData",   xData,  0);
  112.           BoldUnderline(grfx, "May·s",     xShift, 0);
  113.           BoldUnderline(grfx, "Control",   xCtrl,  0);
  114.           BoldUnderline(grfx, "Alt",       xAlt,   0);
  115.  
  116.           if(iNumValid < iNumLines)
  117.           {
  118.                for (int i = 0; i < iNumValid; i++)
  119.                     DisplayKeyInfo(grfx, i, i);
  120.           }
  121.           else
  122.           {
  123.                for (int i = 0; i < iNumLines; i++)
  124.                     DisplayKeyInfo(grfx, i,(iInsertIndex + i) % iNumLines);
  125.           }
  126.      }
  127.      void BoldUnderline(Graphics grfx, string str, int x, int y)
  128.      {
  129.                // Dibujar el texto en negrita.
  130.  
  131.           Brush brush = new SolidBrush(ForeColor);
  132.           grfx.DrawString(str, Font, brush, x, y);
  133.           grfx.DrawString(str, Font, brush, x + 1, y);
  134.  
  135.                // Subrayar el texto.
  136.  
  137.           SizeF sizef = grfx.MeasureString(str, Font);
  138.           grfx.DrawLine(new Pen(ForeColor), x, y + sizef.Height,
  139.                                         x + sizef.Width, y + sizef.Height);
  140.      }
  141.      void DisplayKeyInfo(Graphics grfx, int y, int i)
  142.      {
  143.           Brush br = new SolidBrush(ForeColor);
  144.           y = (1 + y) * Font.Height;    // Convertir y a coordenadas de pφxeles 
  145.  
  146.           grfx.DrawString(akeyevt[i].evttype.ToString(), 
  147.                           Font, br, xEvent, y);
  148.  
  149.           if(akeyevt[i].evttype == EventType.KeyPress)
  150.           {
  151.                KeyPressEventArgs kpea = 
  152.                                    (KeyPressEventArgs) akeyevt[i].evtargs;
  153.  
  154.                string str = String.Format("\x202D{0} (0x{1:X4})", 
  155.                                           kpea.KeyChar, (int) kpea.KeyChar);
  156.                grfx.DrawString(str, Font, br, xChar, y);
  157.           }
  158.           else
  159.           {
  160.                KeyEventArgs kea = (KeyEventArgs) akeyevt[i].evtargs;
  161.  
  162.                string str = String.Format("{0} ({1})", 
  163.                                           kea.KeyCode, (int) kea.KeyCode);
  164.                grfx.DrawString(str, Font, br, xCode, y);
  165.                grfx.DrawString(kea.Modifiers.ToString(), Font, br, xMods, y);
  166.                grfx.DrawString(kea.KeyData.ToString(), Font, br, xData, y);
  167.                grfx.DrawString(kea.Shift.ToString(), Font, br, xShift, y);
  168.                grfx.DrawString(kea.Control.ToString(), Font, br, xCtrl,  y);
  169.                grfx.DrawString(kea.Alt.ToString(), Font, br, xAlt,   y);
  170.           }
  171.      }                         
  172. }
  173.