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("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
cbSize);
[StructLayout(La youtKind.Explic it)]
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("user 32.dll")]
static extern short VkKeyScan(char ch);
[DllImport("user 32.dll")]
static extern void keybd_event(byt e bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
[DllImport("user 32.dll", EntryPoint = "FindWindow ")]
private static extern int FindWindow(stri ng _ClassName, string
_WindowName);
[DllImport("User 32.dll")]
public static extern Int32 SetForegroundWi ndow(int hWnd);
public const int KEYEVENTF_KEYUP = 0x2;
static void Main(string[] args)
{
int i = FindWindow("Not epad", null);
SetForegroundWi ndow(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.dwFla gs = 0;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
mInput.type = 1;
mInput.ki.wVk = keyCode;
mInput.ki.dwFla gs = KEYEVENTF_KEYUP ;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
}
public static void PressKey(char keyCode)
{
const int KEYEVENTF_KEYUP = 0x2;
keybd_event((by te)VkKeyScan(ke yCode), 0x9e, (uint)0, (UIntPtr)0);
keybd_event((by te)VkKeyScan(ke yCode), 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("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
cbSize);
[StructLayout(La youtKind.Explic it)]
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("user 32.dll")]
static extern short VkKeyScan(char ch);
[DllImport("user 32.dll")]
static extern void keybd_event(byt e bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
[DllImport("user 32.dll", EntryPoint = "FindWindow ")]
private static extern int FindWindow(stri ng _ClassName, string
_WindowName);
[DllImport("User 32.dll")]
public static extern Int32 SetForegroundWi ndow(int hWnd);
public const int KEYEVENTF_KEYUP = 0x2;
static void Main(string[] args)
{
int i = FindWindow("Not epad", null);
SetForegroundWi ndow(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.dwFla gs = 0;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
mInput.type = 1;
mInput.ki.wVk = keyCode;
mInput.ki.dwFla gs = KEYEVENTF_KEYUP ;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
}
public static void PressKey(char keyCode)
{
const int KEYEVENTF_KEYUP = 0x2;
keybd_event((by te)VkKeyScan(ke yCode), 0x9e, (uint)0, (UIntPtr)0);
keybd_event((by te)VkKeyScan(ke yCode), 0x9e,
(uint)KEYEVENTF _KEYUP, (UIntPtr)0);
}
Comment