Share via


How can i preform a simulation of a key press/click using send message ?

Question

Tuesday, August 11, 2015 5:10 PM

At the top of form1 i added:

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

const int WM_KEYDOWN = 0x0100;

Then a method i'm calling from a button click event:

private void AutomationIt()
        {
            Process[] GameCapture = Process.GetProcessesByName("GameCapture");
            SetProcessWindow.BringToFront(GameCapture[0].Id);
            SetProcessWindow.CenterProcessWindow(GameCapture[0].Id);            
            if (GameCapture.Length == 0) return;
            if (GameCapture[0] != null)
            {
                IntPtr child = FindWindowEx(GameCapture[0].MainWindowHandle, new IntPtr(0), "Edit", null);
                SendMessage(child, WM_KEYDOWN, 0, "^(r)");
            }
         }

What it does is bringing the process window to the center and to the front.

But it doesn't perform automatic  Ctrl + R click simulation. How can i perform the Ctrl + R combination click ?

All replies (2)

Wednesday, August 12, 2015 12:44 AM âś…Answered

I'm not certain, but I think you need to use WM_CHAR  for the "R" character.

      const uint WM_KEYDOWN = 0x100;
      const uint WM_KEYUP = 0x101;
      const uint WM_CHAR = 0x102;




         SendMessage(handle, WM_KEYDOWN, (int)Keys.ControlKey, 0);
         SendMessage(handle, WM_CHAR, (int)Keys.R, 0);
         SendMessage(handle, WM_KEYUP, (int)Keys.ControlKey, 0);

Tuesday, August 11, 2015 9:44 PM

I changed the code but still not working it's not really doing the ctrl + r it does nothing.

I used a break point i see it's doing the two lines but nothing happen.

At the top of form1 i did:

[DllImport("user32.dll")]
        public static extern int SendMessage(
              IntPtr hWnd,      // handle to destination window  
              uint Msg,       // message  
              int wParam,  // first message parameter  
              int lParam   // second message parameter  
              );

        const uint WM_KEYDOWN = 0x100;
        const uint WM_KEYUP = 0x101;

And method look like this now:

private void AutomationIt()
        {
            Process[] GameCapture = Process.GetProcessesByName("GameCapture");
            SetProcessWindow.BringToFront(GameCapture[0].Id);
            SetProcessWindow.CenterProcessWindow(GameCapture[0].Id);            
            if (GameCapture.Length == 0) return;
            if (GameCapture[0] != null)
            {
                IntPtr handle = GameCapture[0].MainWindowHandle;
                SendMessage(handle, WM_KEYDOWN,(int)Keys.LControlKey, 0);
                SendMessage(handle, WM_KEYDOWN,(int)Keys.R, 0);
            }
        }

I used a break point and it does do the two SendMessage lines but it's not doing anything in the process it self. What's wrong with the SendMessage with the WM_KEYDOWN ? And how do i use the WM_KEUP to release ?