From 4f6d81f7894fed54e962b86d4ab31172512e8e34 Mon Sep 17 00:00:00 2001 From: tech2077 Date: Wed, 16 Oct 2019 22:44:14 -0500 Subject: [PATCH 1/2] Add special case to handle windows buttons as modifiers using virtual-key win32 interface. --- ArcadeControlPanel/SendRawInput.cs | 48 +++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/ArcadeControlPanel/SendRawInput.cs b/ArcadeControlPanel/SendRawInput.cs index c1d4a88..dbf001d 100644 --- a/ArcadeControlPanel/SendRawInput.cs +++ b/ArcadeControlPanel/SendRawInput.cs @@ -92,6 +92,44 @@ struct HARDWAREINPUT // Send a single keystroke public static void SendKey(ushort scanKey, bool KeyDown, bool KeyUp, int delay = 0) { + KEYBDINPUT keybdi; + Console.Out.Write("SendKey: " + scanKey); + Console.Out.Write(" " + KeyDown); + Console.Out.WriteLine(" " + KeyUp); + + // Special case for using virtual-key for windows keys since + // it seems the direct scancode method is broken for them + if (scanKey == (ushort)KeyCodes.WIN || scanKey == (ushort)KeyCodes.RWIN) + { + ushort vk_code; + + // assign virtual-key code + // https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes + if (scanKey == (ushort)KeyCodes.WIN) + { + vk_code = 0x5B; + } else // RWIN + { + vk_code = 0x5C; + } + + keybdi = new KEYBDINPUT + { + wVk = vk_code, + dwFlags = 0 + }; + } else // Direct scancode method + { + keybdi = new KEYBDINPUT + { + wVk = 0, + wScan = scanKey, + dwFlags = KEYEVENTF_SCANCODE, + dwExtraInfo = IntPtr.Zero, + }; + } + + // Not sure why this is an array; investigate later INPUT[] keyPress = new INPUT[] { @@ -100,13 +138,7 @@ public static void SendKey(ushort scanKey, bool KeyDown, bool KeyUp, int delay = type = INPUT_KEYBOARD, u = new InputUnion { - ki = new KEYBDINPUT - { - wVk = 0, - wScan = scanKey, - dwFlags = KEYEVENTF_SCANCODE, - dwExtraInfo = IntPtr.Zero, - } + ki = keybdi } }, }; @@ -126,7 +158,7 @@ public static void SendKey(ushort scanKey, bool KeyDown, bool KeyUp, int delay = if (KeyUp == true) { // key UP - keyPress[0].u.ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE; + keyPress[0].u.ki.dwFlags |= KEYEVENTF_KEYUP; if (SendInput((uint)keyPress.Length, keyPress, Marshal.SizeOf(typeof(INPUT))) == 0) { throw new SendRawInputException("SendInput failed with code: " + Marshal.GetLastWin32Error().ToString()); From fcd82eb071584f86b77bf177e0ad154ae3231945 Mon Sep 17 00:00:00 2001 From: tech2077 Date: Wed, 16 Oct 2019 22:46:49 -0500 Subject: [PATCH 2/2] Add special case to handle windows buttons as modifiers using virtual-key win32 interface. --- ArcadeControlPanel/SendRawInput.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/ArcadeControlPanel/SendRawInput.cs b/ArcadeControlPanel/SendRawInput.cs index dbf001d..05b5a3d 100644 --- a/ArcadeControlPanel/SendRawInput.cs +++ b/ArcadeControlPanel/SendRawInput.cs @@ -93,9 +93,6 @@ struct HARDWAREINPUT public static void SendKey(ushort scanKey, bool KeyDown, bool KeyUp, int delay = 0) { KEYBDINPUT keybdi; - Console.Out.Write("SendKey: " + scanKey); - Console.Out.Write(" " + KeyDown); - Console.Out.WriteLine(" " + KeyUp); // Special case for using virtual-key for windows keys since // it seems the direct scancode method is broken for them