Created
September 15, 2019 15:02
-
-
Save Mijyuoon/024af4af31a9713abc6910903b6ca9dc to your computer and use it in GitHub Desktop.
SendInput crap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace TestProject { | |
public static class WinAPI { | |
public const int GWL_EXSTYLE = -20; | |
public const int WS_EX_NOACTIVATE = 0x08000000; | |
[DllImport("user32.dll")] | |
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int iValue); | |
[DllImport("user32.dll")] | |
public static extern int GetWindowLong(IntPtr hWnd, int nIndex); | |
[DllImport("user32.dll")] | |
public static extern IntPtr GetForegroundWindow(); | |
public const uint MAPVK_VK_TO_SC = 0; | |
public const uint MAPVK_SC_TO_VK = 1; | |
[DllImport("user32.dll")] | |
public static extern uint MapVirtualKey(uint uCode, uint uMapType); | |
[DllImport("user32.dll")] | |
public static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] Input[] pInputs, int cbSize); | |
[DllImport("user32.dll")] | |
public static extern IntPtr GetMessageExtraInfo(); | |
public const int INPUT_MOUSE = 0; | |
public const int INPUT_KEYBOARD = 1; | |
public const int INPUT_HARDWARE = 2; | |
[StructLayout(LayoutKind.Explicit)] | |
public struct Input { | |
[FieldOffset(0)] | |
public int Type; | |
[FieldOffset(4)] | |
public KeyboardInput Ki; | |
[FieldOffset(4)] | |
public MouseInput Mi; | |
[FieldOffset(4)] | |
public HardwareInput Hi; | |
public static Input Keyboard(KeyboardInput input) { | |
return new Input { Type = INPUT_KEYBOARD, Ki = input }; | |
} | |
public static Input Mouse(MouseInput input) { | |
return new Input { Type = INPUT_MOUSE, Mi = input }; | |
} | |
public static Input Hardware(HardwareInput input) { | |
return new Input { Type = INPUT_HARDWARE, Hi = input }; | |
} | |
} | |
public const uint KEYEVENTF_EXTENDED = 0x0001; | |
public const uint KEYEVENTF_KEYUP = 0x0002; | |
public const uint KEYEVENTF_SCANCODE = 0x0008; | |
public struct KeyboardInput { | |
public ushort Vk; | |
public ushort Scan; | |
public uint Flags; | |
public uint Time; | |
public IntPtr ExtraInfo; | |
public KeyboardInput(ushort Vk = 0, ushort Scan = 0, uint Flags = 0, uint Time = 0) { | |
this.Vk = Vk; | |
this.Scan = Scan; | |
this.Flags = Flags; | |
this.Time = Time; | |
ExtraInfo = GetMessageExtraInfo(); | |
} | |
} | |
public struct MouseInput { | |
public int Dx; | |
public int Dy; | |
public uint Data; | |
public uint Flags; | |
public uint Time; | |
public IntPtr ExtraInfo; | |
public MouseInput(int Dx = 0, int Dy = 0, uint Data = 0, uint Flags = 0, uint Time = 0) { | |
this.Dx = Dx; | |
this.Dy = Dy; | |
this.Data = Data; | |
this.Flags = Flags; | |
this.Time = Time; | |
ExtraInfo = GetMessageExtraInfo(); | |
} | |
} | |
public struct HardwareInput { | |
public uint Msg; | |
public ushort ParamL; | |
public ushort ParamH; | |
public HardwareInput(uint Msg = 0, ushort ParamL = 0, ushort ParamH = 0) { | |
this.Msg = Msg; | |
this.ParamL = ParamL; | |
this.ParamH = ParamH; | |
} | |
} | |
public static uint SendInput(Input[] inputs) { | |
return SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<Input>()); | |
} | |
} | |
public class Utils { | |
public static void DisableWindowFocus(Window wnd) { | |
var helper = new WindowInteropHelper(wnd); | |
int value = WinAPI.GetWindowLong(helper.Handle, WinAPI.GWL_EXSTYLE); | |
WinAPI.SetWindowLong(helper.Handle, WinAPI.GWL_EXSTYLE, value | WinAPI.WS_EX_NOACTIVATE); | |
} | |
public static void SendKeyEvent(uint keyCode, bool isDown) { | |
uint flags = WinAPI.KEYEVENTF_SCANCODE; | |
if(!isDown) flags |= WinAPI.KEYEVENTF_KEYUP; | |
if((keyCode & 0x100) > 0) flags |= WinAPI.KEYEVENTF_EXTENDED; | |
ushort scan = (ushort)WinAPI.MapVirtualKey(keyCode & 0xFF, WinAPI.MAPVK_VK_TO_SC); | |
WinAPI.SendInput(new WinAPI.Input[] { | |
WinAPI.Input.Keyboard(new WinAPI.KeyboardInput(Scan: scan, Flags: flags)), | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment