-->

Key strokes in watin

2019-08-07 17:33发布

问题:

I am trying to get my watin app to push the down key but i cant see how to do it. I have seen it in a previous post you should use :

System.Windows.Forms.SendKeys.SendWait("{DOWN}"); 

but this wont work for me as im using a web browser, any suggestions?

回答1:

You need to tell Windows to set it's focus on the Browser Window, and then send the correct key command.

For instance, this code snippet will set focus to the browser and send the "DOWN" key to the browser window.

[DllImport("user32.dll")]
private static extern IntPtr SetFocus(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);

private void SendDownKey(Browser browser)
{
    SetForegroundWindow(browser.document.DomContainer.hWnd);
    SetFocus(browser.document.DomContainer.hWnd);      

    SendKeys.SendWait("{DOWN}");
}


回答2:

System.Windows.Forms.SendKeys.SendWait("{DOWN}"); suggests that you are targetting Windows App. Could you try replacing Windows with Web?

Hope it helps. Let me know if that doesnt work and I will try to dig a bit more into this.



回答3:

This is answered by several good answers in the following question:

Pass a key stroke (i.e., Enter Key) into application using WatiN scripts



标签: watin