Advertisement
Guest User

Untitled

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