Click here to Skip to main content
6,292,426 members and growing! (9,759 online)
Email Password   helpLost your password?
Languages » C# » General     Advanced License: The Code Project Open License (CPOL)

Sending Input Messages to Other Windows or How To Win a Flash Game By Your Programming Skills

By Michal Remi

Library for sending input messages to other windows and links to libraries you need to take full control over any running window.
C#, Windows, .NET, Visual Studio, Dev
Posted:4 Jul 2006
Views:21,858
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 2.96 Rating: 3.28 out of 5

1
1 vote, 12.5%
2
3 votes, 37.5%
3
2 votes, 25.0%
4
2 votes, 25.0%
5
Sample Image - take-over.jpg

Introduction

This article presents a class, handling sending of input (mouse and keyboard) to any running Windows Control by its handle.

Background

Some time ago, I was making a program which would win a penalty shootout soccer flash game for me. My task in the application was repeatedly to do these things:

  • Grab flash window content
  • Do some image processing and find out what to do
  • Finally, take some action (send mouse input)

I found some great articles concerning taking of screenshot like this one.

This library helped me with image processing a lot.

On the other hand, I spent a lot of time trying to find out how to send mouse input to flash window correctly... and when I got how to do it, I decided to make this library.

Get the Handle

In order to use the TakeOver class, you need to obtain the handle (int number) of the window you want to control. It's possible to use spy++ tool from Visual Studio and convert HEX value to DEC using Windows calculator. The other possibility is to implement your own Window Picker (like in the demo project). Further details about this problem are out of scope for this article.

Using the Code

The first thing you need to do to take control over the window is to create a TakeOver class instance.

Remo.TakeOver tO = new Remo.TakeOver(targetWindowHandle);

Sending of input messages to a window is quite self-explanatory. The class provides these methods for sending messages:

public void SendLeftButtonDown(int x,int y);
public void SendLeftButtonUp(int x,int y);
public void SendLeftButtonDblClick(int x,int y);
public void SendRightButtonDown(int x,int y);
public void SendRightButtonUp(int x,int y);
public void SendRightButtonDblClick(int x,int y);
public void SendMouseMove(int x,int y);
public void SendKeyDown(int key);
public void SendKeyUp(int key);
public void SendChar(char c);
public void SendString(string s);

Sending input usually requires a target window to be focused to work properly, however it's not always so. The demo application can be tested here. Normally, you will have the application running in the background with controlled window focused, so there should be no problem with sending input messages. Since keyboard messages are not very reliable (I do not know how to encode lParam of SendMessage correctly, any hints?), it is recommended for keyboard input to use the SendKeys class once the window is focused via the SetFocus() method. Sendkeys is part of the standard System.Windows.Forms namespace. To focus target window TakeOver class provides a method:

public void SetFocus();

History

  • 4th July, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Michal Remi


Member
I am one step before graduating Faculty of Mathematics, Physics and Informatics, Commenius University Bratislava, specialization Computer Graphics. Working as C# programmer on large IS. Creator of Ubytovanie na Slovensku site.
Occupation: Web Developer
Location: Slovakia Slovakia

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
Questiona trouble with a game using keyboard control Pinmembermixiaojion23:33 20 Sep '07  
Generalsending mouse messages to flash Pinmemberadamani15:25 1 May '07  
Generalgames Pinmember-m-rt--n-3:45 28 Oct '06  
QuestionInteresting ... PinmemberOBalert11:49 20 Jul '06  
AnswerRe: Interesting ... PinmemberMichal Remiš4:32 23 Jul '06  
QuestionNice article! PinmemberWestves23:13 5 Jul '06  
AnswerRe: Nice article! [modified] PinmemberMichal Remiš11:35 6 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Jul 2006
Editor: Deeksha Shenoy
Copyright 2006 by Michal Remi
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project