Guest User

Untitled

a guest
May 18th, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Input;
  5.  
  6. namespace Test
  7. {
  8.     class KeyboardManager
  9.     {
  10.         internal static class NativeMethods
  11.         {
  12.             [Flags]
  13.             public enum KeyboardFlags : uint
  14.             {
  15.                 None = 0,
  16.  
  17.                 /// <summary>
  18.                 /// KEYEVENTF_EXTENDEDKEY = 0x0001 (If specified, the scan code was preceded by a prefix byte that has the value 0xE0 (224).)
  19.                 /// </summary>
  20.                 ExtendedKey = 1,
  21.  
  22.                 /// <summary>
  23.                 /// KEYEVENTF_KEYUP = 0x0002 (If specified, the key is being released. If not specified, the key is being pressed.)
  24.                 /// </summary>
  25.                 KeyUp = 2,
  26.  
  27.                 /// <summary>
  28.                 /// KEYEVENTF_UNICODE = 0x0004 (If specified, wScan identifies the key and wVk is ignored.)
  29.                 /// </summary>
  30.                 Unicode = 4,
  31.  
  32.                 /// <summary>
  33.                 /// KEYEVENTF_SCANCODE = 0x0008 (Windows 2000/XP: If specified, the system synthesizes a VK_PACKET keystroke. The wVk parameter must be zero. This flag can only be combined with the KEYEVENTF_KEYUP flag. For more information, see the Remarks section.)
  34.                 /// </summary>
  35.                 ScanCode = 8,
  36.             }
  37.  
  38.             [Flags]
  39.             public enum MouseFlags : uint
  40.             {
  41.                 /// <summary>
  42.                 /// Specifies that movement occurred.
  43.                 /// </summary>
  44.                 Move = 0x0001,
  45.  
  46.                 /// <summary>
  47.                 /// Specifies that the left button was pressed.
  48.                 /// </summary>
  49.                 LeftDown = 0x0002,
  50.  
  51.                 /// <summary>
  52.                 /// Specifies that the left button was released.
  53.                 /// </summary>
  54.                 LeftUp = 0x0004,
  55.  
  56.                 /// <summary>
  57.                 /// Specifies that the right button was pressed.
  58.                 /// </summary>
  59.                 RightDown = 0x0008,
  60.  
  61.                 /// <summary>
  62.                 /// Specifies that the right button was released.
  63.                 /// </summary>
  64.                 RightUp = 0x0010,
  65.  
  66.                 /// <summary>
  67.                 /// Specifies that the middle button was pressed.
  68.                 /// </summary>
  69.                 MiddleDown = 0x0020,
  70.  
  71.                 /// <summary>
  72.                 /// Specifies that the middle button was released.
  73.                 /// </summary>
  74.                 MiddleUp = 0x0040,
  75.  
  76.                 /// <summary>
  77.                 /// Windows 2000/XP: Specifies that an X button was pressed.
  78.                 /// </summary>
  79.                 XDown = 0x0080,
  80.  
  81.                 /// <summary>
  82.                 /// Windows 2000/XP: Specifies that an X button was released.
  83.                 /// </summary>
  84.                 XUp = 0x0100,
  85.  
  86.                 /// <summary>
  87.                 /// Windows NT/2000/XP: Specifies that the wheel was moved, if the mouse has a wheel. The amount of movement is specified in mouseData.
  88.                 /// </summary>
  89.                 VerticalWheel = 0x0800,
  90.  
  91.                 /// <summary>
  92.                 /// Specifies that the wheel was moved horizontally, if the mouse has a wheel. The amount of movement is specified in mouseData. Windows 2000/XP:  Not supported.
  93.                 /// </summary>
  94.                 HorizontalWheel = 0x1000,
  95.  
  96.                 /// <summary>
  97.                 /// Windows 2000/XP: Maps coordinates to the entire desktop. Must be used with MOUSEEVENTF_ABSOLUTE.
  98.                 /// </summary>
  99.                 VirtualDesk = 0x4000,
  100.  
  101.                 /// <summary>
  102.                 /// Specifies that the dx and dy members contain normalized absolute coordinates. If the flag is not set, dxand dy contain relative data (the change in position since the last reported position). This flag can be set, or not set, regardless of what kind of mouse or other pointing device, if any, is connected to the system. For further information about relative mouse motion, see the following Remarks section.
  103.                 /// </summary>
  104.                 Absolute = 0x8000,
  105.             }
  106.  
  107.             [StructLayout(LayoutKind.Sequential)]
  108.             public struct KEYBDINPUT
  109.             {
  110.                 public ushort virtualKey;
  111.                 public ushort scanCode;
  112.                 public KeyboardFlags flags;
  113.                 public uint timeStamp;
  114.                 public IntPtr extraInfo;
  115.             }
  116.  
  117.             [StructLayout(LayoutKind.Sequential)]
  118.             public struct MOUSEINPUT
  119.             {
  120.                 public int deltaX;
  121.                 public int deltaY;
  122.                 public uint mouseData;
  123.                 public MouseFlags flags;
  124.                 public uint time;
  125.                 public IntPtr extraInfo;
  126.             }
  127.  
  128.             [StructLayout(LayoutKind.Sequential)]
  129.             public struct HARDWAREINPUT
  130.             {
  131.                 public uint message;
  132.                 public ushort wParamL;
  133.                 public ushort wParamH;
  134.             }
  135.  
  136.             [StructLayout(LayoutKind.Explicit)]
  137.             public struct InputUnion
  138.             {
  139.                 [FieldOffset(0)]
  140.                 public MOUSEINPUT mouse;
  141.                 [FieldOffset(0)]
  142.                 public KEYBDINPUT keyboard;
  143.                 [FieldOffset(0)]
  144.                 public HARDWAREINPUT hardware;
  145.             }
  146.             public enum InputType : int
  147.             {
  148.                 Mouse = 0,
  149.                 Keyboard = 1,
  150.                 Hardware = 2
  151.             }
  152.             public struct INPUT
  153.             {
  154.                 public InputType type;
  155.                 public InputUnion union;
  156.             }
  157.  
  158.             public static int SizeOfInput { get; } = Marshal.SizeOf(typeof(INPUT));
  159.  
  160.             [DllImport("user32.dll")]
  161.             public static extern uint SendInput(int nInputs, INPUT[] pInputs, int cbSize);
  162.         }
  163.  
  164.         public static void PressHotkey(Key key, ModifierKeys modifiers = ModifierKeys.None)
  165.         {
  166.             static NativeMethods.INPUT BuildINPUT(Key k, NativeMethods.KeyboardFlags flags) => new NativeMethods.INPUT
  167.             {
  168.                 type = NativeMethods.InputType.Keyboard,
  169.                 union = new NativeMethods.InputUnion { keyboard = new NativeMethods.KEYBDINPUT { virtualKey = (ushort)KeyInterop.VirtualKeyFromKey(k), scanCode = 0, flags = flags, timeStamp = 0, extraInfo = IntPtr.Zero } }
  170.             };
  171.             List<Key> keys = new List<Key>();
  172.             if (modifiers.HasFlag(ModifierKeys.Control)) keys.Add(Key.LeftCtrl);
  173.             if (modifiers.HasFlag(ModifierKeys.Alt)) keys.Add(Key.LeftAlt);
  174.             if (modifiers.HasFlag(ModifierKeys.Shift)) keys.Add(Key.LeftShift);
  175.             if (modifiers.HasFlag(ModifierKeys.Windows)) keys.Add(Key.LWin);
  176.             keys.Add(key);
  177.             NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[keys.Count * 2];
  178.             for (int i = 0; i < keys.Count; i++)
  179.             {
  180.                 inputs[i] = BuildINPUT(keys[i], NativeMethods.KeyboardFlags.None);
  181.                 inputs[^(i + 1)] = BuildINPUT(keys[i], NativeMethods.KeyboardFlags.KeyUp);
  182.             }
  183.             _ = NativeMethods.SendInput(inputs.Length, inputs, NativeMethods.SizeOfInput);
  184.         }
  185.     }
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment