0

I'm trying basically SendKey's to IE9 to change tabs. I have 3 tabs so I'd need to Send keys Ctrl+1, Ctrl+2, Ctrl+3 and also Ctrl+T to open a new tab.

I start by adding the import dlls and constants

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
 IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg,
 IntPtr wParam, IntPtr lParam);
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;

I get the instance of Internet Explorer by opening a new process.

Process p = Process.Start("iexplorer.exe");

Use the process handle to PostMessage to IE9 instance

IntPtr handle = p.MainWindowHandle; //p.Handle (doesn't work either)
//Change to Tab2 using PostMessage

PostMessage(handle, WM_KEYDOWN, ((IntPtr)Keys.LControlKey), (IntPtr)0);
PostMessage(handle, WM_KEYDOWN, ((IntPtr)Keys.D2), (IntPtr)0);
PostMessage(handle, WM_KEYUP, ((IntPtr)Keys.D2), (IntPtr)0);
PostMessage(handle, WM_KEYUP, ((IntPtr)Keys.LControlKey), (IntPtr)0);

No response. I've also tried using SendMessage to no avail as well.

Am I doing anything obviously wrong?

Andre Walker
  • 79
  • 1
  • 1
  • 11

1 Answers1

0

How about SendKeys("^1");

as seen here

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • Boo thanks for the response. I'm familiar with SendKeys but I don't want to use it because it requires focus. The MainWindowHandle is the proper handle as I'm trying to send keystroke to the main containing Window. – Andre Walker Apr 23 '12 at 22:05
  • the container window might not be the one that processes keyboard input. I'd be surprised if it was. – Sam Axe Apr 24 '12 at 03:37
  • I'm just going to mark this as this obvously wrong answer because I need the points. Either this can't be done or people don't want to share how it's done. – Andre Walker Apr 24 '12 at 16:58