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?

标签: watin
3条回答
何必那么认真
2楼-- · 2019-08-07 17:53

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}");
}
查看更多
叛逆
3楼-- · 2019-08-07 17:57

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

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

查看更多
Summer. ? 凉城
4楼-- · 2019-08-07 18:03

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.

查看更多
登录 后发表回答