?
Solved

Spoofing keyboard/mouse-clicks on other applications?

Posted on 2003-03-31
1
Medium Priority
?
397 Views
Last Modified: 2012-06-27
I'd like my C# program to be able to spoof a keyboard <Enter> or a mouse click on an "OK" button in another application.  Is this possible?  how?

thanks!
-Michael
0
Comment
Question by:sassafrasy
1 Comment
 
LVL 2

Accepted Solution

by:
ericgu earned 1000 total points
ID: 8249382
To do this from C#, you need to use SendMessage() or PostMessage() from Win32. If you go to http://www.csharp.net and goto the code section, you'll find a class named Win32Window that has definitions that allow you to do this.

I've also pulled out some code samples out of a utility that I wrote. They should give you a flavor for what to do, but they're pretty rough.

          static int BM_CLICK = 0x00F5;
          private void timer1_Tick(object sender, System.EventArgs e)
          {
               Win32Window window = Win32Window.FindWindow(null, "Microsoft Outlook");

#if fred
               Win32Window test = window.LastActivePopup;
               if (!test.IsNull)
               {
                    IntPtr i = test.Window;
                    IntPtr j = i;
               }
#endif

#if !fred
                    // check to see if there's a candidate window...                    
            if (!window.IsNull)
               {
                    int threadId = window.ThreadId;

                    foreach (Win32Window w in Win32Window.GetThreadWindows(threadId))
                    {
                         if (w.Text == "Microsoft Outlook")
                         {
                              IntPtr i = w.Window;
                              IntPtr j = i;

                              WindowPlacement placement;
                              placement = window.WindowPlacement;
                              int state = placement.showCmd;
                              TriageAndKill(w);
                         }
                    }
               }
#endif
#if !fred
               if (!window.IsNull)
               {

                    TriageAndKill(window);
               }
#endif
          }

          void TriageAndKill(Win32Window window)
          {
               // check that it has an OK button and no Cancel button
               Win32Window okButton = window.FindChild("Button", "OK");
               Win32Window cancelButton = window.FindChild("Button", "Cancel");

               if (!okButton.IsNull && cancelButton.IsNull)
               {

                    // Walk through all the child windows, and look for long text. Anything more than 10 characters is
                    // considered to be a message...
                    foreach (Win32Window child in window.Children)
                    {
                         string s = child.Text;
                         if (s.Length > 10)
                         {
                              int count = 0;
                              if (messages.Contains(s))
                              {
                                   count = (int) messages[s];
                              }
                              count++;
                              messages[s] = count++;
                         }
                    }

                    window.BringWindowToTop();
                    okButton.SendMessage(BM_CLICK, 0, 0);
                    killCount++;
                    this.notifyIcon1.Text = String.Format("Outlook Popup Killer \n{0} killed", killCount);
               }
          }
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Integration Management Part 2
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question