code_junkie

Sending Keyboard Macro Commands to Game Windows

Nov 13th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. Private Const WM_KEYDOWN As Integer = &H100
  2. Private Const WM_KEYUP As Integer = &H101
  3.  
  4. private static int WM_KEYDOWN = 0x100
  5. private static int WM_KEYUP = 0x101
  6.  
  7. class SendKeySample
  8. {
  9. private static Int32 WM_KEYDOWN = 0x100;
  10. private static Int32 WM_KEYUP = 0x101;
  11.  
  12. [return: MarshalAs(UnmanagedType.Bool)]
  13. [DllImport("user32.dll", SetLastError = true)]
  14. static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam);
  15.  
  16. [DllImport("user32.dll", SetLastError = true)]
  17. private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  18.  
  19. public static IntPtr FindWindow(string windowName)
  20. {
  21. foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
  22. {
  23. if (p.MainWindowHandle != IntPtr.Zero && p.MainWindowTitle.ToLower() == windowName.ToLower())
  24. return p.MainWindowHandle;
  25. }
  26.  
  27. return IntPtr.Zero;
  28. }
  29.  
  30. public static IntPtr FindWindow(IntPtr parent, string childClassName)
  31. {
  32. return FindWindowEx(parent, IntPtr.Zero, childClassName, string.Empty);
  33. }
  34.  
  35. public static void SendKey(IntPtr hWnd, System.Windows.Forms.Keys key)
  36. {
  37. PostMessage(hWnd, WM_KEYDOWN, key, 0);
  38.  
  39. }
  40. }
  41.  
  42. var hWnd = SendKeySample.FindWindow("Untitled - Notepad");
  43. var editBox = SendKeySample.FindWindow(hWnd, "edit");
  44.  
  45. SendKeySample.SendKey(editBox, Keys.A);
Add Comment
Please, Sign In to add comment