simulating-key-press-c-sharp


SUBMITTED BY: menamagice

DATE: Aug. 9, 2017, 1:57 a.m.

FORMAT: ANTLR With C# Target

SIZE: 1.1 kB

HITS: 313

  1. Here's an example...
  2. static class Program
  3. {
  4. [DllImport("user32.dll")]
  5. public static extern int SetForegroundWindow(IntPtr hWnd);
  6. [STAThread]
  7. static void Main()
  8. {
  9. while(true)
  10. {
  11. Process [] processes = Process.GetProcessesByName("iexplore");
  12. foreach(Process proc in processes)
  13. {
  14. SetForegroundWindow(proc.MainWindowHandle);
  15. SendKeys.SendWait("{F5}");
  16. }
  17. Thread.Sleep(5000);
  18. }
  19. }
  20. }
  21. a better one... less anoying...
  22. static class Program
  23. {
  24. const UInt32 WM_KEYDOWN = 0x0100;
  25. const int VK_F5 = 0x74;
  26. [DllImport("user32.dll")]
  27. static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
  28. [STAThread]
  29. static void Main()
  30. {
  31. while(true)
  32. {
  33. Process [] processes = Process.GetProcessesByName("iexplore");
  34. foreach(Process proc in processes)
  35. PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);
  36. Thread.Sleep(5000);
  37. }
  38. }
  39. }

comments powered by Disqus