Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating Key Press C#

Tags:

c#

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?

like image 755
Dani Avatar asked Jun 15 '10 17:06

Dani


People also ask

How do you automate key pressing?

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.

How do you check if a key is pressed in C?

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.

How do you press a key in C++?

ki. wVk = 0x41; // virtual-key code for the "a" key ip. ki. dwFlags = 0; // 0 for key press SendInput(1, &ip, sizeof(INPUT)); // ...


1 Answers

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);         }     } } 
like image 102
vakuras Avatar answered Sep 30 '22 19:09

vakuras