G
Guest
I'm trying to figure out the fastest/easiest way of sending text to another
application. I've got the SendInput and keybd_event methods working well
enough... is one better than the other? Is there something else I should try
out? Is there a way to send text to another window without giving that
window focus? Below is the code I'm using.
Thanks in advance.
*****************************
[DllImport("user32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
public struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
HARDWAREINPUT hi;
}
struct MOUSEINPUT
{
int dx;
int dy;
int mouseData;
int dwFlags;
int time;
IntPtr dwExtraInfo;
}
public struct KEYBDINPUT
{
public char wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
struct HARDWAREINPUT
{
int uMsg;
short wParamL;
short wParamH;
}
[DllImport("user32.dll")]
static extern short VkKeyScan(char ch);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string _ClassName, string
_WindowName);
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
public const int KEYEVENTF_KEYUP = 0x2;
static void Main(string[] args)
{
int i = FindWindow("Notepad", null);
SetForegroundWindow(i);
string str1 = "CRAIG";
for (int j = 0; j < str1.Length-1; j++)
{
PressKey((char)str1[j]);
}
for (int j = 0; j < str1.Length; j++)
{
PressKey2(str2[j]);
}
}
}
}
public static void PressKey2(char keyCode)
{
INPUT mInput = new INPUT();
KEYBDINPUT ki = new KEYBDINPUT();
mInput.type = 1;
mInput.ki = ki;
mInput.ki.wVk = keyCode;
mInput.ki.dwFlags = 0;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
mInput.type = 1;
mInput.ki.wVk = keyCode;
mInput.ki.dwFlags = KEYEVENTF_KEYUP;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
}
public static void PressKey(char keyCode)
{
const int KEYEVENTF_KEYUP = 0x2;
keybd_event((byte)VkKeyScan(keyCode), 0x9e, (uint)0, (UIntPtr)0);
keybd_event((byte)VkKeyScan(keyCode), 0x9e,
(uint)KEYEVENTF_KEYUP, (UIntPtr)0);
}
application. I've got the SendInput and keybd_event methods working well
enough... is one better than the other? Is there something else I should try
out? Is there a way to send text to another window without giving that
window focus? Below is the code I'm using.
Thanks in advance.
*****************************
[DllImport("user32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
public struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
HARDWAREINPUT hi;
}
struct MOUSEINPUT
{
int dx;
int dy;
int mouseData;
int dwFlags;
int time;
IntPtr dwExtraInfo;
}
public struct KEYBDINPUT
{
public char wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
struct HARDWAREINPUT
{
int uMsg;
short wParamL;
short wParamH;
}
[DllImport("user32.dll")]
static extern short VkKeyScan(char ch);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string _ClassName, string
_WindowName);
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
public const int KEYEVENTF_KEYUP = 0x2;
static void Main(string[] args)
{
int i = FindWindow("Notepad", null);
SetForegroundWindow(i);
string str1 = "CRAIG";
for (int j = 0; j < str1.Length-1; j++)
{
PressKey((char)str1[j]);
}
for (int j = 0; j < str1.Length; j++)
{
PressKey2(str2[j]);
}
}
}
}
public static void PressKey2(char keyCode)
{
INPUT mInput = new INPUT();
KEYBDINPUT ki = new KEYBDINPUT();
mInput.type = 1;
mInput.ki = ki;
mInput.ki.wVk = keyCode;
mInput.ki.dwFlags = 0;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
mInput.type = 1;
mInput.ki.wVk = keyCode;
mInput.ki.dwFlags = KEYEVENTF_KEYUP;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
}
public static void PressKey(char keyCode)
{
const int KEYEVENTF_KEYUP = 0x2;
keybd_event((byte)VkKeyScan(keyCode), 0x9e, (uint)0, (UIntPtr)0);
keybd_event((byte)VkKeyScan(keyCode), 0x9e,
(uint)KEYEVENTF_KEYUP, (UIntPtr)0);
}