Simulating Key Press c#

后端 未结 8 1390
青春惊慌失措
青春惊慌失措 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

提交回复
热议问题