Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace AssociationTask2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             bool found;
  12.             Input input = new Input();
  13.             input.AddListener(ConsoleKey.W, () =>
  14.             {
  15.                 Console.WriteLine("W press");
  16.             });
  17.             Console.WriteLine("Для выхода из цикла нажмите клавишу - Escape!");
  18.             Thread thread = new Thread(() =>
  19.             {
  20.                 while (true) input.Update();
  21.             });
  22.             thread.Start();
  23.  
  24.             while (input.Session)
  25.             {
  26.                
  27.                 //
  28.             }
  29.         }
  30.  
  31.         static void CallBack(WaitForButton waitFor)
  32.         {
  33.             waitFor(Console.ReadKey().Key);
  34.         }
  35.     }
  36.  
  37.     delegate void WaitForButton(ConsoleKey key);
  38.     delegate Action SomeReaction(ConsoleKey key, out bool value);
  39.  
  40.     class Input
  41.     {
  42.         private Dictionary<ConsoleKey, Action> _dictionary;
  43.         private SomeReaction _someReaction;
  44.         private WaitForButton _makeSubscribe;
  45.         private bool _session;
  46.  
  47.         public Input()
  48.         {
  49.             Session = true;
  50.             Dictionary = new Dictionary<ConsoleKey, Action>();
  51.             Dictionary.Add(ConsoleKey.Escape, () => Session = false);
  52.             SomeReaction = GetReaction;
  53.             MakeSubscribe = SubscriptionOnKey;
  54.         }
  55.  
  56.  
  57.         public Dictionary<ConsoleKey, Action> Dictionary { get => _dictionary; set => _dictionary = value; }
  58.         public SomeReaction SomeReaction { get => _someReaction; set => _someReaction = value; }
  59.         public WaitForButton MakeSubscribe { get => _makeSubscribe; set => _makeSubscribe = value; }
  60.         public bool Session { get => _session; set => _session = value; }
  61.  
  62.         public void AddListener(ConsoleKey key, Action onPress)
  63.         {
  64.             if (_dictionary.ContainsKey(key))
  65.             {
  66.                 _dictionary[key] += onPress;
  67.             }
  68.             else
  69.             {
  70.                 _dictionary.Add(key, onPress);
  71.             }
  72.         }
  73.  
  74.         private void SubscriptionOnKey(ConsoleKey consoleKey)
  75.         {
  76.             Console.WriteLine($"Введите реакцию на клавишу - {consoleKey}:");
  77.             string reaction = Console.ReadLine();
  78.             Dictionary.Add(consoleKey, () =>
  79.             {
  80.                 Console.WriteLine("\n" + reaction);
  81.             });
  82.         }
  83.  
  84.         private bool LookForReaction(ConsoleKey consoleKey, out Action action)
  85.         {
  86.             return Dictionary.TryGetValue(consoleKey, out action);
  87.         }
  88.  
  89.         public void Update()
  90.         {
  91.             ConsoleKeyInfo key = Console.ReadKey(false);
  92.             if (_dictionary.ContainsKey(key.Key))
  93.             {
  94.                 _dictionary[key.Key].Invoke();
  95.             }
  96.         }
  97.  
  98.         private Action GetReaction(ConsoleKey consoleKey, out bool found)
  99.         {
  100.             Action action;
  101.             found = LookForReaction(consoleKey, out action);
  102.             if (found)
  103.                 return action;
  104.             return () => Console.WriteLine($"\nНа клавишу - {consoleKey.ToString().ToLower()} нет подписки!");
  105.         }
  106.  
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement