views:

392

answers:

4

I would like to send keys to a c# web browser control element directly, not just the window with the correct focus.

Right now I am able to send a key using PostMessage and documentHandle and focus like this

i.e. Pseudo Code

HtmlElement el = getelement();
el.Focus();
IntPtr docptr = browser.Handle;
PostMessage(docptr,WM_KEYDOWN,1,0);
.... KEYCHAR, KEYUP..

I was wondering if anyone knows of any way to be able to do this in a background, so that the focus is not on the element. Basically is there a way to get a IntPtr to the HtmlElement itself, than use that instead in the PostMessage or SendKeys API rather than the browser handle/ptr.

A: 

This is the wrong way to go.

You can use the HtmlElement API to manipulate the element directly.

For precise instructions, please tell us what you're trying to do.

SLaks
This is for a specific application of emulating key strokes, to for example trigger javascript code for testing. I realize you can .InvokeMemeber("click"), or .SetAttribute("value", value). But this is not what I'm talking about here.
Jakub
A: 

Have a look at things like WaitN and Selenium.

IE also has a good programming interface to control the browser.

But there are downsides...

I'm currently working on a program that uses image recognition and control of the mouse/keys to control a browser as I can only really do what I want with the visual representation.

However that will dominate the computer and need full control. Unless you run it in a virtual machine.

Keith Nicholas
The question above is about the IE Interface. I'm looking for a .NET/C#/WIN32 solution to sending keys to a HtmlElement; specifically avoiding setting focusing on the control and sending the message to the whole window. I'm asking if anyone has sendkeys directly to the control instead.
Jakub
if you can find out the control for the element you want you can send a message to it and set the text directly rather than send keys to it
Keith Nicholas
A: 

You can't. Those are windowless controls, mouse/keyboard messages are handled by their parent window, and the parent only relay message for the active control.

Sheng Jiang 蒋晟
thanks that's what I wanted to know.
Jakub