Advertisement
Mijyuoon

SendInput

Oct 22nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. public static class WinAPI {
  2.     public const uint MAPVK_VK_TO_SC = 0;
  3.     public const uint MAPVK_SC_TO_VK = 1;
  4.  
  5.     [DllImport("user32.dll")]
  6.     public static extern uint MapVirtualKey(uint uCode, uint uMapType);
  7.  
  8.     [DllImport("user32.dll")]
  9.     public static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] Input[] pInputs, int cbSize);
  10.  
  11.     [DllImport("user32.dll")]
  12.     public static extern IntPtr GetMessageExtraInfo();
  13.  
  14.     public const int INPUT_MOUSE    = 0;
  15.     public const int INPUT_KEYBOARD = 1;
  16.     public const int INPUT_HARDWARE = 2;
  17.  
  18.     [StructLayout(LayoutKind.Explicit)]
  19.     public struct Input {
  20.         [FieldOffset(0)]
  21.         public int Type;
  22.  
  23.         [FieldOffset(4)]
  24.         public KeyboardInput Ki;
  25.  
  26.         [FieldOffset(4)]
  27.         public MouseInput Mi;
  28.  
  29.         [FieldOffset(4)]
  30.         public HardwareInput Hi;
  31.  
  32.         public static Input Keyboard(KeyboardInput input) {
  33.             return new Input { Type = INPUT_KEYBOARD, Ki = input };
  34.         }
  35.  
  36.         public static Input Mouse(MouseInput input) {
  37.             return new Input { Type = INPUT_MOUSE, Mi = input };
  38.         }
  39.  
  40.         public static Input Hardware(HardwareInput input) {
  41.             return new Input { Type = INPUT_HARDWARE, Hi = input };
  42.         }
  43.     }
  44.  
  45.     public const uint KEYEVENTF_EXTENDED = 0x0001;
  46.     public const uint KEYEVENTF_KEYUP    = 0x0002;
  47.     public const uint KEYEVENTF_SCANCODE = 0x0008;
  48.  
  49.     public struct KeyboardInput {
  50.         public ushort Vk;
  51.         public ushort Scan;
  52.         public uint Flags;
  53.         public uint Time;
  54.         public IntPtr ExtraInfo;
  55.  
  56.         public KeyboardInput(ushort Vk = 0, ushort Scan = 0, uint Flags = 0, uint Time = 0) {
  57.             this.Vk = Vk;
  58.             this.Scan = Scan;
  59.             this.Flags = Flags;
  60.             this.Time = Time;
  61.             ExtraInfo = GetMessageExtraInfo();
  62.         }
  63.     }
  64.  
  65.     public struct MouseInput {
  66.         public int Dx;
  67.         public int Dy;
  68.         public uint Data;
  69.         public uint Flags;
  70.         public uint Time;
  71.         public IntPtr ExtraInfo;
  72.  
  73.         public MouseInput(int Dx = 0, int Dy = 0, uint Data = 0, uint Flags = 0, uint Time = 0) {
  74.             this.Dx = Dx;
  75.             this.Dy = Dy;
  76.             this.Data = Data;
  77.             this.Flags = Flags;
  78.             this.Time = Time;
  79.             ExtraInfo = GetMessageExtraInfo();
  80.         }
  81.     }
  82.  
  83.     public struct HardwareInput {
  84.         public uint Msg;
  85.         public ushort ParamL;
  86.         public ushort ParamH;
  87.  
  88.         public HardwareInput(uint Msg = 0, ushort ParamL = 0, ushort ParamH = 0) {
  89.             this.Msg = Msg;
  90.             this.ParamL = ParamL;
  91.             this.ParamH = ParamH;
  92.         }
  93.     }
  94.  
  95.     public static uint SendInput(Input[] inputs) {
  96.         return SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<Input>());
  97.     }
  98. }
  99.  
  100. public class Utils {
  101.     public static void SendKeyEvent(uint keyCode, bool isDown) {
  102.         uint flags = WinAPI.KEYEVENTF_SCANCODE;
  103.  
  104.         if(!isDown) flags |= WinAPI.KEYEVENTF_KEYUP;
  105.         if((keyCode & 0x100) > 0) flags |= WinAPI.KEYEVENTF_EXTENDED;
  106.  
  107.         ushort scan = (ushort)WinAPI.MapVirtualKey(keyCode & 0xFF, WinAPI.MAPVK_VK_TO_SC);
  108.  
  109.         WinAPI.SendInput(new WinAPI.Input[] {
  110.             WinAPI.Input.Keyboard(new WinAPI.KeyboardInput(Scan: scan, Flags: flags)),
  111.         });
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement