Advertisement
xZ3ROxPROJ3CTx

Virtual Keyboard Control

Aug 21st, 2012
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10.  
  11.     //------------------
  12.     //Creator: aeonhack
  13.     //Converted by: recuperare
  14.     //Site: elitevs.net
  15.     //Created: 03/25/2013
  16.     //Changed: 03/25/2013
  17.     //Version: 1.0.0
  18.     //------------------
  19.  
  20.     class VirtualKeyboard : Control
  21.     {
  22.  
  23.         private Bitmap TextBitmap;
  24.  
  25.         private Graphics TextGraphics;
  26.         const string LowerKeys = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
  27.  
  28.         const string UpperKeys = "~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?";
  29.         public VirtualKeyboard()
  30.         {
  31.             SetStyle((ControlStyles)139270, true);
  32.  
  33.             Font = new Font("Verdana", 8.25f);
  34.  
  35.             TextBitmap = new Bitmap(1, 1);
  36.             TextGraphics = Graphics.FromImage(TextBitmap);
  37.  
  38.             MinimumSize = new Size(386, 162);
  39.             MaximumSize = new Size(386, 162);
  40.  
  41.             Randomize();
  42.             PrepareCache();
  43.         }
  44.  
  45.         private Control Target;
  46.         public void AssignControl(Control c)
  47.         {
  48.             Target = c;
  49.         }
  50.  
  51.  
  52.         private bool Shift;
  53.         private int Pressed = -1;
  54.  
  55.         private Rectangle[] Buttons;
  56.         private char[] Lower;
  57.         private char[] Upper;
  58.         private string[] Other = {
  59.         "Shift",
  60.         "Space",
  61.         "Back"
  62.  
  63.     };
  64.         private void PrepareCache()
  65.         {
  66.             Buttons = new Rectangle[51];
  67.  
  68.             int I = 0;
  69.  
  70.             for (int Y = 0; Y <= 3; Y++)
  71.             {
  72.                 for (int X = 0; X <= 11; X++)
  73.                 {
  74.                     I = (Y * 12) + X;
  75.                     Buttons[I] = new Rectangle(X * 32, Y * 32, 32, 32);
  76.                 }
  77.             }
  78.  
  79.             Buttons[48] = new Rectangle(0, 4 * 32, 2 * 32, 32);
  80.             Buttons[49] = new Rectangle(Buttons[48].Right, 4 * 32, 8 * 32, 32);
  81.             Buttons[50] = new Rectangle(Buttons[49].Right, 4 * 32, 2 * 32, 32);
  82.         }
  83.  
  84.         private void Randomize()
  85.         {
  86.             Lower = LowerKeys.ToCharArray();
  87.             Upper = UpperKeys.ToCharArray();
  88.  
  89.             Random R = new Random();
  90.             int X = 0;
  91.             int Y = 0;
  92.             char C1 = '\0';
  93.             char C2 = '\0';
  94.  
  95.             for (int I = 1; I <= 12; I++)
  96.             {
  97.                 X = R.Next(Lower.Length);
  98.                 Y = R.Next(Lower.Length);
  99.  
  100.                 C1 = Lower[X];
  101.                 C2 = Upper[X];
  102.  
  103.                 Lower[X] = Lower[Y];
  104.                 Upper[X] = Upper[Y];
  105.  
  106.                 Lower[Y] = C1;
  107.                 Upper[Y] = C2;
  108.             }
  109.         }
  110.  
  111.         private Graphics G;
  112.         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  113.         {
  114.             G = e.Graphics;
  115.             G.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  116.  
  117.             G.Clear(SystemColors.Control);
  118.  
  119.             SizeF S = default(SizeF);
  120.             Rectangle R = default(Rectangle);
  121.             int Offset = 0;
  122.  
  123.             G.DrawRectangle(SystemPens.ControlDarkDark, 0, 0, (12 * 32) + 1, (5 * 32) + 1);
  124.  
  125.             for (int I = 0; I <= Buttons.Length - 1; I++)
  126.             {
  127.                 R = Buttons[I];
  128.  
  129.                 Offset = 0;
  130.                 if (I == Pressed)
  131.                     Offset = 1;
  132.  
  133.                 switch (I)
  134.                 {
  135.                     case 48:
  136.                     case 49:
  137.                     case 50:
  138.                         S = G.MeasureString(Other[I - 48], Font);
  139.                         G.DrawString(Other[I - 48], Font, SystemBrushes.ControlText, R.X + (R.Width / 2 - S.Width / 2) + Offset, R.Y + (R.Height / 2 - S.Height / 2) + Offset);
  140.                         break;
  141.                     case 47:
  142.                         DrawArrow(R.X + Offset, R.Y + Offset);
  143.                         break;
  144.                     default:
  145.                         if (Shift)
  146.                         {
  147.                             G.DrawString(Upper[I].ToString(), Font, SystemBrushes.ControlText, R.X + 4 + Offset, R.Y + 3 + Offset);
  148.                         }
  149.                         else
  150.                         {
  151.                             G.DrawString(Lower[I].ToString(), Font, SystemBrushes.ControlText, R.X + 4 + Offset, R.Y + 3 + Offset);
  152.                         }
  153.                         break;
  154.                 }
  155.  
  156.                 G.DrawRectangle(SystemPens.ControlLightLight, R.X + 1 + Offset, R.Y + 1 + Offset, R.Width - 2, R.Height - 2);
  157.                 G.DrawRectangle(SystemPens.ControlDark, R.X + Offset, R.Y + Offset, R.Width, R.Height);
  158.  
  159.                 if (I == Pressed)
  160.                 {
  161.                     G.DrawLine(SystemPens.ControlDarkDark, R.X, R.Y, R.Right, R.Y);
  162.                     G.DrawLine(SystemPens.ControlDarkDark, R.X, R.Y, R.X, R.Bottom);
  163.                 }
  164.             }
  165.         }
  166.  
  167.         private void DrawArrow(int rx, int ry)
  168.         {
  169.             Rectangle R = new Rectangle(rx + 8, ry + 8, 16, 16);
  170.             G.SmoothingMode = SmoothingMode.AntiAlias;
  171.  
  172.             Pen P = new Pen(SystemColors.ControlText, 1);
  173.             AdjustableArrowCap C = new AdjustableArrowCap(3, 2);
  174.             P.CustomEndCap = C;
  175.  
  176.             G.DrawArc(P, R, 0f, 290f);
  177.  
  178.             P.Dispose();
  179.             C.Dispose();
  180.             G.SmoothingMode = SmoothingMode.None;
  181.         }
  182.  
  183.         protected override void OnMouseDown(MouseEventArgs e)
  184.         {
  185.             int Index = ((e.Y / 32) * 12) + (e.X / 32);
  186.  
  187.             if (Index > 47)
  188.             {
  189.                 for (int I = 48; I <= Buttons.Length - 1; I++)
  190.                 {
  191.                     if (Buttons[I].Contains(e.X, e.Y))
  192.                     {
  193.                         Pressed = I;
  194.                         break; // TODO: might not be correct. Was : Exit For
  195.                     }
  196.                 }
  197.             }
  198.             else
  199.             {
  200.                 Pressed = Index;
  201.             }
  202.  
  203.             HandleKey();
  204.             Invalidate();
  205.         }
  206.  
  207.         protected override void OnMouseUp(MouseEventArgs e)
  208.         {
  209.             Pressed = -1;
  210.             Invalidate();
  211.         }
  212.  
  213.         private void HandleKey()
  214.         {
  215.             if (Target == null)
  216.                 return;
  217.  
  218.             switch (Pressed)
  219.             {
  220.                 case 47:
  221.                     Randomize();
  222.                     break;
  223.                 case 48:
  224.                     Shift = !Shift;
  225.                     break;
  226.                 case 49:
  227.                     Target.Text += " ";
  228.                     break;
  229.                 case 50:
  230.                     if (!(Target.Text.Length == 0))
  231.                     {
  232.                         Target.Text = Target.Text.Remove(Target.Text.Length - 1);
  233.                     }
  234.                     break;
  235.                 default:
  236.                     if (Shift)
  237.                     {
  238.                         Target.Text += Upper[Pressed];
  239.                     }
  240.                     else
  241.                     {
  242.                         Target.Text += Lower[Pressed];
  243.                     }
  244.                     break;
  245.             }
  246.         }
  247.  
  248.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement