Simulating Key Press c#

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

    Easy, short and no need window focus:

    Also here a usefull list of Virtual Key Codes

            [DllImport("user32.dll")]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll")]
            static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
    
            private void button1_Click(object sender, EventArgs e)
            {
                const int WM_SYSKEYDOWN = 0x0104;
                const int VK_F5 = 0x74;
    
                IntPtr WindowToFind = FindWindow(null, "Google - Mozilla Firefox");
    
                PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_F5, 0);
            }
    

提交回复
热议问题