Advertisement
zhangsongcui

ConsoleKbhitEvent

Jul 21st, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4.  
  5. class ConsoleKbhit
  6. {
  7.     public class ConsoleKbhitEventArgs
  8.     {
  9.         public ConsoleKbhitEventArgs()
  10.         {
  11.             isCancel = false;
  12.             Console.CursorVisible = false;
  13.         }
  14.  
  15.         public bool isCancel
  16.         {
  17.             get { return cancel == 1; }
  18.             set { Interlocked.Exchange(ref cancel, value ? 1 : 0); }
  19.         }
  20.  
  21.         public ConsoleKeyInfo keyInfo { get; set; }
  22.         volatile int cancel;
  23.     }
  24.  
  25.     public void StartListening()
  26.     {
  27.         if (ConsoleKbhitEvent == null)
  28.             throw new ArgumentNullException("需先绑定事件:ConsoleKbhitEvent");
  29.         trd = new Thread(new ThreadStart(() =>
  30.         {
  31.             do
  32.             {
  33.                 args.keyInfo = Console.ReadKey(true);
  34.                 ConsoleKbhitEvent(this, args);
  35.             } while (!args.isCancel);
  36.         }));
  37.         trd.Start();
  38.     }
  39.  
  40.     public bool isListening
  41.     {
  42.         get { return trd != null && trd.ThreadState == ThreadState.Running; }
  43.     }
  44.  
  45.     public void MarkStop()
  46.     {
  47.         args.isCancel = true;
  48.     }
  49.  
  50.     private ConsoleKbhitEventArgs args = new ConsoleKbhitEventArgs();
  51.     private Thread trd;
  52.  
  53.     public delegate void ConsoleKbhitEventHandler(object sender, ConsoleKbhitEventArgs e);
  54.     public event ConsoleKbhitEventHandler ConsoleKbhitEvent;
  55. }
  56.  
  57. class Program
  58. {
  59.     const short Width = 10, Height = 20;
  60.  
  61.     static void GotoAndPut(int X, int Y, char C)
  62.     {
  63.         Console.CursorLeft = X;
  64.         Console.CursorTop = Y;
  65.         Console.Write(C);
  66.     }
  67.  
  68.     static void Main()
  69.     {
  70.         var kbhit = new ConsoleKbhit();
  71.         kbhit.ConsoleKbhitEvent += new ConsoleKbhit.ConsoleKbhitEventHandler(kbhit_ConsoleKbhitEvent);
  72.         kbhit.StartListening();
  73.         Console.Write('■');
  74.         while (kbhit.isListening)
  75.         {
  76.             Thread.Sleep(500);
  77.             lock(thisLock)
  78.             {
  79.                 if (Y < Height - 1)
  80.                     MoveTo(delegate { Y++; });
  81.                 else
  82.                     X = Y = 0;
  83.             }
  84.         }
  85.     }
  86.  
  87.     static void MoveTo(Action f)
  88.     {
  89.         GotoAndPut(X, Y, ' ');
  90.         f();
  91.         GotoAndPut(X, Y, '■');
  92.     }
  93.  
  94.     static void kbhit_ConsoleKbhitEvent(object sender, ConsoleKbhit.ConsoleKbhitEventArgs e)
  95.     {
  96.         if (e.keyInfo.Key == ConsoleKey.Escape || e.keyInfo.Key == ConsoleKey.Q)
  97.         {
  98.             e.isCancel = true;
  99.             return;
  100.         }
  101.         lock (thisLock)
  102.         {
  103.             switch (e.keyInfo.Key)
  104.             {
  105.                 case ConsoleKey.W: break;
  106.                 case ConsoleKey.S: if (Y < Height - 1) MoveTo(delegate { Y++; }); else X = Y = 0; break;
  107.                 case ConsoleKey.A: MoveTo(delegate { if (X > 0) X -= 2; }); break;
  108.                 case ConsoleKey.D: MoveTo(delegate { if (X < Width * 2 - 2) X += 2; }); break;
  109.             }
  110.         }
  111.     }
  112.  
  113.     static object thisLock = new object();
  114.     static int X = 0, Y = 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement