I have a problem with this project in C#: When using WinAPI SendInput function
/// <summary>
/// Sends Unicode (UTF16) string to foreground window.
/// </summary>
/// <param name="inputString">String to be sent to foreground window.</param>
internal static void Send(string inputString)
{
if (inputString == string.Empty)
{ return; }
char[] chars = inputString.ToCharArray();
INPUT[] pInputs = new INPUT[chars.Length * 2];
for (int i = 0; i < chars.Length; i++)
{
UInt16 unicode = chars[i];
pInputs[i * 2] = new INPUT();
pInputs[i * 2].type = INPUT_KEYBOARD;
pInputs[i * 2].ki.wVk = 0;
pInputs[i * 2].ki.wScan = unicode;
pInputs[i * 2].ki.dwFlags = KEYEVENTF_UNICODE;
pInputs[i * 2].ki.time = 0;
pInputs[i * 2].ki.dwExtraInfo = SetMessageExtraInfo(IntPtr.Zero);
pInputs[i * 2 + 1] = new INPUT();
pInputs[i * 2 + 1].type = INPUT_KEYBOARD;
pInputs[i * 2 + 1].ki.wVk = 0;
pInputs[i * 2 + 1].ki.wScan = unicode;
pInputs[i * 2 + 1].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
pInputs[i * 2 + 1].ki.time = 0;
pInputs[i * 2 + 1].ki.dwExtraInfo = SetMessageExtraInfo(IntPtr.Zero);
}
uint nSent = SendInput((uint)chars.Length * 2, pInputs, Marshal.SizeOf(typeof(INPUT)));
if (nSent == 0)
{
Debug.WriteLine("SendInput error " + GetLastError().ToString()); // error 87 : "The parameter is incorrect."
}
}
In desktop applications like Notepad or VS the code works fine, but in others it doesn't work with the simple English alphabet and punctuation. A string like "íáó" with special characters works explicitly/universally, but a string like "My car" doesn't. Obviously, the utf-16 values for the chars in "My car" are low, less than 100; the values for á and í are 225 and 237, respectively. Seemingly a superficial difference. Anyone know of a way to get regular English letters to be sent as Unicode to arbitrary windows using SendInput?