private static bool SendVKeyNative32(int vKey, char? optUnicodeChar, bool bDown) { NativeMethods.INPUT32[] pInput = new NativeMethods.INPUT32[1]; pInput[0].Type = NativeMethods.INPUT_KEYBOARD; if(optUnicodeChar.HasValue && WinUtil.IsAtLeastWindows2000) { pInput[0].KeyboardInput.VirtualKeyCode = 0; pInput[0].KeyboardInput.ScanCode = (ushort)optUnicodeChar.Value; pInput[0].KeyboardInput.Flags = ((bDown ? 0 : NativeMethods.KEYEVENTF_KEYUP) | NativeMethods.KEYEVENTF_UNICODE); } else // Standard VKey { if(optUnicodeChar.HasValue) vKey = (int)(NativeMethods.VkKeyScan(optUnicodeChar.Value) & 0xFF); pInput[0].KeyboardInput.VirtualKeyCode = (ushort)vKey; pInput[0].KeyboardInput.ScanCode = (ushort)(NativeMethods.MapVirtualKey((uint)vKey, 0) & 0xFF); pInput[0].KeyboardInput.Flags = ((bDown ? 0 : NativeMethods.KEYEVENTF_KEYUP) | (IsExtendedKeyEx(vKey) ? NativeMethods.KEYEVENTF_EXTENDEDKEY : 0)); } pInput[0].KeyboardInput.Time = 0; pInput[0].KeyboardInput.ExtraInfo = NativeMethods.GetMessageExtraInfo(); Debug.Assert(Marshal.SizeOf(typeof(NativeMethods.INPUT32)) == 28); if(NativeMethods.SendInput32(1, pInput, Marshal.SizeOf(typeof(NativeMethods.INPUT32))) != 1) return false; return true; }
private static bool SendModifierVKey32Unchecked(int vKey, bool bDown) { NativeMethods.INPUT32[] pInput = new NativeMethods.INPUT32[1]; pInput[0].Type = NativeMethods.INPUT_KEYBOARD; pInput[0].KeyboardInput.VirtualKeyCode = (ushort)vKey; pInput[0].KeyboardInput.ScanCode = (ushort)(NativeMethods.MapVirtualKey((uint)vKey, 0) & 0xFF); pInput[0].KeyboardInput.Flags = ((bDown ? 0 : NativeMethods.KEYEVENTF_KEYUP) | (IsExtendedKeyEx(vKey) ? NativeMethods.KEYEVENTF_EXTENDEDKEY : 0)); pInput[0].KeyboardInput.Time = 0; pInput[0].KeyboardInput.ExtraInfo = NativeMethods.GetMessageExtraInfo(); Debug.Assert(Marshal.SizeOf(typeof(NativeMethods.INPUT32)) == 28); if(NativeMethods.SendInput32(1, pInput, Marshal.SizeOf(typeof(NativeMethods.INPUT32))) != 1) return false; return true; }