Using SENDINPUT to press virtual keys works perfect for my application on any open field. Great that's what it is for :) However. Recently a bug report was made that it doesn't work in a RDP session. So I fire up notepad on my computer scan a barcode (it's how my SENDINPUT is used) and the string gets put up on notepad. I start a RDP session open up notepad and nothing gets sent. Notepad remains blank. So for some strange reason the SENDINPUT is not going through the RDP tunnel. I have a feeling it is more of a setting in the RDP session, but i'm not so naive as to think that my code can't be at fault.The code is nothing special but I'll post the important part (the actual call to SEND INPUT)
public class User32Input
{
public static void DoPressRawKey(byte ascii)
{
var inputs = PressRawKey(ascii);
int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(INPUT));
SendInput((uint)inputs.Count, inputs.ToArray(), size);
}
internal static System.Collections.Generic.List<INPUT> PressRawKey(byte ascii)
{
var inputs = new System.Collections.Generic.List<INPUT>();
inputs.Add(GetRawKeyDown(ascii));
inputs.Add(GetRawKeyUp(ascii));
return inputs;
}
internal static INPUT GetRawKeyDown(byte key)
{
return GetRawKey(key, KEYEVENTF_KEYDN);
}
internal static INPUT GetRawKeyUp(byte key)
{
return GetRawKey(key, KEYEVENTF_KEYUP);
}
private static INPUT GetRawKey(byte key, uint flag)
{
return new INPUT
{
type = User32Input.INPUT_KEYBOARD,
u = new InputUnion
{
ki = new KEYBDINPUT
{
wVk = key,
wScan = 0,
dwFlags = flag,
dwExtraInfo = User32Input.GetMessageExtraInfo(),
}
}
};
}
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
}
Any ideas as to why it's not working in a RDP session?
Set the scancode, RDP or ICA sessions - requires KEYBDINPUT.Scan to be non-zero. Use MapVirtualKey to get it