Avatar of l4zarus
l4zarus
Flag for United States of America asked on

C# SendMessage Keypress

I've been trying to send keypresses to another application using SendMessage in C#. So far I've gotten absolutely no results from it. I've tried sending different combinations of these functions to different applications with no luck.
ex)
SendKey("Untitled - Notepad", Keys.A);
#region Function Imports
 
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
        [DllImport("user32.dll")]
        static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
 
        #endregion
 
        #region Constants
 
        // Messages
        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int WM_CHAR = 0x105;
        const int WM_SYSKEYDOWN = 0x104;
        const int WM_SYSKEYUP = 0x105;
 
        #endregion
 
        public static void SendKey(string wName, Keys key)
        {
            IntPtr hWnd = FindWindow(null, wName);
 
            SendMessage(hWnd, WM_KEYDOWN, Convert.ToInt32(key), 0);
            SendMessage(hWnd, WM_KEYUP, Convert.ToInt32(key), 0);
        }
 
        public static void SendSysKey(string wName, Keys key)
        {
            IntPtr hWnd = FindWindow(null, wName);
 
            SendMessage(hWnd, WM_SYSKEYDOWN, Convert.ToInt32(key), 0);
            SendMessage(hWnd, WM_SYSKEYUP, Convert.ToInt32(key), 0);
        }
 
        public static void SendChar(string wName, char c)
        {
            IntPtr hWnd = FindWindow(null, wName);
 
            SendMessage(hWnd, WM_CHAR, (int)c, 0);
        }

Open in new window

C#

Avatar of undefined
Last Comment
mcwllc

8/22/2022 - Mon
Dirk Haest

Are you sure that hWnd contains a value ?

To use SendMessage() you have to retrieve the window handle hWnd of the application you want to talk to.
In fact I would try the following:
Use FindWindow() or FindWindowEx() API to retrieve the hWnd,
then, before any usage of SendKey() you set the focus to the app, using SendMessage()

Const WM_SETFOCUS As Long = &H7
  dim hWnd as Long
  hWnd = FindWindow("", "your window title")
  If hWnd then SendMessage hWnd, WM_SETFOCUS, 0, 0

Open in new window

l4zarus

ASKER
I'm fairly sure that hWnd is correct. I'm using notepad as a test.

            IntPtr hwnd = InputSender.FindWindow(null, "Untitled - Notepad");
            addText("Handle: " + hwnd.ToString());
            StringBuilder title = new StringBuilder(256);
            InputSender.GetWindowText(hwnd, title, 256);
            addText(title.ToString());

Outputs:

Handle: 4327520
Untitled - Notepad

...

WM_SETFOCUS (0x7) doesn't seem to be working either...
l4zarus

ASKER
Okay. I just added in a GetLastError() right after the SendMessage and it gave me code 6 (ERROR_INVALID_HANDLE). Seems to be the problem after all. I'm not sure how to fix it though.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
alb66

You must activate the application using SetForgroundWindow() not SetFocus().
http://msdn.microsoft.com/en-us/library/ms171548(VS.80).aspx
mastoo

Yes, I'd imagine activating the app is the problem but thought I'd mention you can use the Spy utility to help diagnose this.  It can confirm the windows handle (compare it to what your program is trying to use) and it will display messages being sent.
ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
l4zarus

ASKER
Working like a charm. One last thing.

If I wanted to send a combination (such as Shift+A or Alt+F4) how does that work. I thought all I had to do for alt+f4 was PostMessage(editWnd, 0x104, (int)Keys.F4, 0) but this didn't exit Notepad. Doing a keydown shift and then keydown A didn't make it a shift+A either :(
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mike Tomlinson

To be honest I don't know how to key combinations (or if it's even possible) with PostMessage().
mcwllc

for combos:
don't keyup the first key in the sequence until you keydown all you wish to be pressed