Simulating Key Press c#

后端 未结 8 1153
青春惊慌失措
青春惊慌失措 2020-11-22 05:48

I want to simulate F5 key press in my c# program, When IE is open I want to be able refresh my website automatically.

相关标签:
8条回答
  • 2020-11-22 06:19

    Simple one, add before Main

    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    

    Code inside Main/Method:

    string className = "IEFrame";
    string windowName = "New Tab - Windows Internet Explorer";
    IntPtr IE = FindWindow(className, windowName);
    if (IE == IntPtr.Zero)
    {
       return;
    }
    SetForegroundWindow(IE);
    InputSimulator.SimulateKeyPress(VirtualKeyCode.F5);
    

    Note:

    1. Add InputSimulator as reference. To download Click here

    2. To find Class & Window name, use WinSpy++. To download Click here

    0 讨论(0)
  • 2020-11-22 06:19

    Another alternative to simulating a F5 key press would be to simply host the WebBrowser control in the Window Forms application. You use the WebBrowser.Navigate method to load your web page and then use a standard Timer and on each tick of the timer you just re-Navigate to the url which will reload the page.

    0 讨论(0)
提交回复
热议问题