AutoITには必要な機能があることに気付いたので、ソースファイルsendKeys.cppを調べて、この関数の次のC ++コードスニペットを見つけました。C#に変換するのは簡単です。
scan = MapVirtualKey(vk, 0);
// Build the generic lparam to be used for WM_KEYDOWN/WM_KEYUP/WM_CHAR
lparam = 0x00000001 | (LPARAM)(scan << 16); // Scan code, repeat=1
if (bForceExtended == true || IsVKExtended(vk) == true)
lparam = lparam | 0x01000000; // Extended code if required
if ( (m_nKeyMod & ALTMOD) && !(m_nKeyMod & CTRLMOD) ) // Alt without Ctrl
PostMessage(m_hWnd, WM_SYSKEYDOWN, vk, lparam | 0x20000000); // Key down, AltDown=1
else
PostMessage(m_hWnd, WM_KEYDOWN, vk, lparam); // Key down
スキャンコードはMapVirtualKeyで生成できます
C#翻訳:
public static void sendKey(IntPtr hwnd, VKeys keyCode, bool extended)
{
uint scanCode = MapVirtualKey((uint)keyCode, 0);
uint lParam;
//KEY DOWN
lParam = (0x00000001 | (scanCode << 16));
if (extended)
{
lParam |= 0x01000000;
}
PostMessage(hwnd, (UInt32)WMessages.WM_KEYDOWN, (IntPtr)keyCode, (IntPtr)lParam);
//KEY UP
lParam |= 0xC0000000; // set previous key and transition states (bits 30 and 31)
PostMessage(hwnd, WMessages.WM_KEYUP, (uint)keyCode, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);