I am having a strange problem with SendKeys.Send
Basically what happens. I have Internet Explorer focused on google.com and I call SendKeys.Send ("TestSample \ n"); it sometimes sends some keys twice (for example, TeestSample or TestSSSample) in an unpredictable way. This occurs in approximately 20% of cases.
In addition, when I include a space in the SendKeys.Send line ("Test Sample \ n"), it is also unpredictable, except for one point. Each time I do this, it enters a test sample, performs a search on Google, but also scrolls the results page, because after entering the text I hit the space bar.
Has anyone else seen this behavior. This does not seem to work with focus recording.
(To illustrate a few code examples here. Put it in one second of the timer in the form with the DllImport definitions at the top of the class.) This application does not work in about 20% of cases using Google.com in Internet Explorer (8.0)
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); private void timer1_Tick(object sender, EventArgs e) { IntPtr foreground = GetForegroundWindow(); if (foreground != _currentForeground) { _currentForeground = foreground; var titleBuilder = new StringBuilder(200); GetWindowText(foreground, titleBuilder, 200); string title = titleBuilder.ToString(); Debug.WriteLine("Title of " + title); if (title == "Google - Windows Internet Explorer") { Debug.WriteLine("Sending keys"); SendKeys.Send("Test Sample\n"); } if (title == "Untitled - Notepad") SendKeys.Send("Test notpad sample\n"); Thread.Sleep(2000); } } private IntPtr _currentForeground = IntPtr.Zero;
source share