Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using AutoHotkey.Interop;
- using System.Threading.Tasks;
- namespace ConsoleApplication
- {
- class Program
- {
- private static void Main(string[] args)
- {
- var keyboardSimulator = new KeyboardSimulator();
- keyboardSimulator.PressButton("a"); // нажать кнопку a
- }
- }
- class KeyboardSimulator : IDisposable
- {
- private AutoHotkey.Interop.AutoHotkeyEngine _ahk;
- public void Dispose()
- {
- _ahk = null;
- }
- public KeyboardSimulator()
- {
- _ahk = new AutoHotkeyEngine();
- }
- public void PressButton(string key)
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("sendinput, {").Append(GetAhkVk(key)).Append('}');
- _ahk.ExecRaw(sb.ToString());
- }
- public async void AsyncPressButton(string key)
- {
- await Task.Run(new Action(delegate
- {
- PressButton(key);
- }));
- }
- private string GetAhkVk(string key)
- {
- var _key = key.ToLower();
- for (int i = 0; i < _ahkButtons.Length; i++)
- {
- if (_key == _normalButtons[i])
- return _ahkButtons[i];
- }
- return "";
- }
- private readonly string[] _ahkButtons = new string[]
- {
- "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
- "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
- "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
- "sc3A","sc1E","sc1F","sc20","sc21","sc22","sc23","sc24","sc25","sc26","sc27","sc28","sc2B",// caps a s d f g h j k l ; ' \
- "sc2A","sc2C","sc2D","sc2E","sc2F","sc30","sc31","sc32","sc33","sc34","sc35","sc36", // lshift z x c v b n m , . / rshift
- "sc1D","sc15B","sc38","sc39", "sc36" // ctrl win alt space shift
- };
- private readonly string[] _normalButtons = new[]
- {
- "esc","f1","f2","f3","f4","f5","f6","f7","f8","f9","f10","f11","f12",
- "`","1","2","3","4","5","6","7","8","9","0","-","=","backspace",
- "tab","q","w","e","r","t","y","u","i","o","p","[","]","enter",
- "caps","a","s","d","f","g","h","j","k","l",";","'","\\",
- "lshift","z","x","c","v","b","n","m",",",".","/","rshift",
- "ctrl","win","alt","space", "shift"
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement