public KeyInputForm() { InitializeComponent(); var keys = new WinFormsKeyEventSource(this); this.keys = keys; textBox1.KeyDown += OnKeyDown; textBox1.KeyPress += OnKeyPress; }
public static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); form = new Form1(); colors = new Brush[] { Brushes.Black, Brushes.Blue, Brushes.Green, Brushes.Yellow, Brushes.Gray, Brushes.Orange, Brushes.Purple, Brushes.White }; game = new TetrisGame(10, 20); buffer = new Bitmap(form.ClientSize.Width, form.ClientSize.Height); back = Graphics.FromImage(buffer); front = form.CreateGraphics(); last = DateTime.Now; block = Properties.Resources.block; bgimage = Properties.Resources.back; thread = new Thread(new ThreadStart(Run)); var keys = new WinFormsKeyEventSource(form); keys.AddKeyAlias("up", Keys.Up); keys.AddKeyAlias("down", Keys.Down); keys.AddKeyAlias("left", Keys.Left); keys.AddKeyAlias("right", Keys.Right); var keyActions = new Dictionary <string, Action <bool> >() { ["up"] = game.SetFlip, ["down"] = game.SetDrop, ["left"] = game.SetLeft, ["right"] = game.SetRight }; keys.Changed += delegate(object sender, KeyChangeEventArgs args) { keyActions[args.Name](args.State); }; keys.Start(); thread.Start(); Application.Run(form); keys.Quit(); thread.Abort(); buffer.Dispose(); block.Dispose(); bgimage.Dispose(); back.Dispose(); front.Dispose(); form.Dispose(); }