I want to simulate F5 key press in my C# program. When IE is open, I want to be able refresh my website automatically.
How can I do that?
To automate any keypress, you can use the press command. To specify a single keyboard character, use the character itself. For example, to automate pressing the letter 'a', use the command "press a" (without quotes). Similarly, to press a sequence of keys just pass them in order.
kbhit() is present in conio. h and used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file “conio.
ki. wVk = 0x41; // virtual-key code for the "a" key ip. ki. dwFlags = 0; // 0 for key press SendInput(1, &ip, sizeof(INPUT)); // ...
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); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With