Simulating Key Press c#

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

    Here's an example...

    static class Program
    {
        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr hWnd);
    
        [STAThread]
        static void Main()
        {
            while(true)
            {
                Process [] processes = Process.GetProcessesByName("iexplore");
    
                foreach(Process proc in processes)
                {
                    SetForegroundWindow(proc.MainWindowHandle);
                    SendKeys.SendWait("{F5}");
                }
    
                Thread.Sleep(5000);
            }
        }
    }
    

    a better one... less anoying...

    static class Program
    {
        const UInt32 WM_KEYDOWN = 0x0100;
        const int VK_F5 = 0x74;
    
        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
    
        [STAThread]
        static void Main()
        {
            while(true)
            {
                Process [] processes = Process.GetProcessesByName("iexplore");
    
                foreach(Process proc in processes)
                    PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);
    
                Thread.Sleep(5000);
            }
        }
    }
    

提交回复
热议问题