[DllImport("user32.dll")]
static extern short VkKeyScan(char ch);
[StructLayout( LayoutKind.Explicit )]
public struct INPUT
{
[FieldOffset( 0 )]
public int type;
[FieldOffset( 4 )]
public KEYBDINPUT ki;
}
[StructLayout( LayoutKind.Sequential )]
public struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
None.
Please add some!
The VkKeyScanEx function allows to specify a locale.
This function allows you to map the contents of a string or a Textbox into keystrokes, as shown in the sample code
This sample code shows how to read the text from a string and send it as keystrokes to Windows. It uses the SetForegroundWindow, SendInput, VkKeyScan functions.
void Main () {
string text = "abc123"; // text we will send
Process notepad = Process.Start( "notepad" ); // launch notepad, we'll write into it
Thread.Sleep( 2000 ); //give notepad some time to launch
SetForegroundWindow( notepad.MainWindowHandle );// bring that notepad to the foreground
short key = 0; //variable where we will hold the value of each key
// create an INPUT structure with default values
INPUT input = new INPUT();
input.type = INPUT_KEYBOARD;
input.ki = new KEYBDINPUT();
input.ki.dwExtraInfo = GetMessageExtraInfo();
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.wScan = 0;
foreach ( char c in text ) {
key = VkKeyScan( c ); //get the key for this character
input.ki.wVk = (ushort) key;//update the input structure
SendInput( 1, ref input, Marshal.SizeOf( input ) );//send the key to notepad
}
}
[DllImport( "user32.dll" )]
public static extern bool SetForegroundWindow (IntPtr hWnd);
[DllImport( "user32.dll", SetLastError = true )]
public static extern uint SendInput (uint nInputs, ref INPUT pInputs, int cbSize);
public const int INPUT_KEYBOARD = 1;
[DllImport( "user32.dll" )]
public static extern IntPtr GetMessageExtraInfo ();
Do you know one? Please contribute it!