Sendkeys problem from .NET program
Posted
by user203123
on Stack Overflow
See other posts from Stack Overflow
or by user203123
Published on 2010-04-09T01:06:23Z
Indexed on
2010/04/09
1:23 UTC
Read the original article
Hit count: 367
THe code below I copied from MSDN with a bit of modification:
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
DllImport("User32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
int cnt = 0;
private void button1_Click(object sender, EventArgs e)
{
IntPtr calculatorHandle = FindWindow("Notepad", "Untitled - Notepad");
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait(cnt.ToString());
SendKeys.SendWait("{ENTER}");
cnt++;
SendKeys.Flush();
System.Threading.Thread.Sleep(1000);
}
The problem is the number sequence in Notepad is not continuously. The first click always results 0 (as expected). but from the second click, the result is unpredictable (but the sequence is still in order, e.g. 3, 4, 5, 10, 14, 15, ....)
If I click the button fast enough, I was able to get the result in continuous order (0,1,2,3,4,....) but sometimes it produces more than 2 same numbers (e.g. 0,1,2,3,3,3,4,5,6,6,6,7,8,9,...)
© Stack Overflow or respective owner