?
Solved

Spoofing keyboard/mouse-clicks on other applications?

Posted on 2003-03-31
1
Medium Priority
?
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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