Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using AutoHotkey.Interop;
  4. using System.Threading.Tasks;
  5.  
  6. namespace ConsoleApplication
  7. {
  8.     class Program
  9.     {
  10.         private static void Main(string[] args)
  11.         {
  12.             var keyboardSimulator = new KeyboardSimulator();
  13.             keyboardSimulator.PressButton("a"); // нажать кнопку a
  14.         }      
  15.     }
  16.  
  17.     class KeyboardSimulator : IDisposable
  18.     {
  19.         private AutoHotkey.Interop.AutoHotkeyEngine _ahk;
  20.  
  21.         public void Dispose()
  22.         {
  23.             _ahk = null;
  24.         }
  25.  
  26.         public KeyboardSimulator()
  27.         {
  28.             _ahk = new AutoHotkeyEngine();
  29.         }
  30.  
  31.         public void PressButton(string key)
  32.         {
  33.             StringBuilder sb = new StringBuilder();
  34.             sb.Append("sendinput, {").Append(GetAhkVk(key)).Append('}');
  35.  
  36.             _ahk.ExecRaw(sb.ToString());
  37.         }
  38.  
  39.         public async void AsyncPressButton(string key)
  40.         {
  41.             await Task.Run(new Action(delegate
  42.             {
  43.                 PressButton(key);
  44.             }));
  45.         }
  46.  
  47.         private string GetAhkVk(string key)
  48.         {
  49.             var _key = key.ToLower();
  50.  
  51.             for (int i = 0; i < _ahkButtons.Length; i++)
  52.             {
  53.                 if (_key == _normalButtons[i])
  54.                     return _ahkButtons[i];
  55.             }
  56.  
  57.             return "";
  58.         }
  59.  
  60.         private readonly string[] _ahkButtons = new string[]
  61.         {
  62.             "sc1","sc3B","sc3C","sc3D","sc3E","sc3F","sc40","sc41","sc42","sc43","sc44","sc57","sc58", // esc f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
  63.             "sc29","sc2","sc3","sc4","sc5","sc6","sc7","sc8","sc9","scA","scB","scC","scD","scE", // ` 1 2 3 4 5 6 7 8 9 0 - = backspace
  64.             "scF","sc10","sc11","sc12","sc13","sc14","sc15","sc16","sc17","sc18","sc19","sc1A","sc1B","sc1C", // tab q w e r t y u i o p [ ] enter
  65.             "sc3A","sc1E","sc1F","sc20","sc21","sc22","sc23","sc24","sc25","sc26","sc27","sc28","sc2B",// caps a s d f g h j k l ; ' \
  66.             "sc2A","sc2C","sc2D","sc2E","sc2F","sc30","sc31","sc32","sc33","sc34","sc35","sc36", // lshift z x c v b n m , . / rshift
  67.             "sc1D","sc15B","sc38","sc39", "sc36"  // ctrl win alt space shift
  68.         };
  69.  
  70.         private readonly string[] _normalButtons = new[]
  71.         {
  72.             "esc","f1","f2","f3","f4","f5","f6","f7","f8","f9","f10","f11","f12",
  73.             "`","1","2","3","4","5","6","7","8","9","0","-","=","backspace",
  74.             "tab","q","w","e","r","t","y","u","i","o","p","[","]","enter",
  75.             "caps","a","s","d","f","g","h","j","k","l",";","'","\\",
  76.             "lshift","z","x","c","v","b","n","m",",",".","/","rshift",
  77.             "ctrl","win","alt","space", "shift"
  78.         };
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement