troubleshooting Question

C# SendMessage Keypress

Avatar of l4zarus
l4zarusFlag for United States of America asked on
C#
9 Comments1 Solution34343 ViewsLast Modified:
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);
        }
ASKER CERTIFIED SOLUTION
Mike Tomlinson
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros