Skip to main content

Keyboard and Mouse Simulator in C sharp .NET


Lets design a robot program which can simulate mouse and keyboard events on your desktop. Simulator which can automate the keyboard and mouse functions for you, can do almost all possible tasks on your desktop which ranges from setting up a task in the Task Scheduler to booking a movie ticket for you. You can smartly use and customize this piece of code to perform hectic tasks which you have to perform repeatedly like checking email.
Subscribe us for our YouTube channel and any kind of help


We shall start with basics of how to simulate keyboard and mouse using c sharp, .NET. 

Creating Mouse Events

1. Importing user32.dll from Win32 API

[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int MOUSEEVENTF_MOVE = 0x0001; /* mouse move */
private const int MOUSEEVENTF_LEFTDOWN = 0x0002; /* left button down */
private const int MOUSEEVENTF_LEFTUP = 0x0004; /* left button up */
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; /* right button down */

declare the function and other members of the user32.dll we are going to use. 

2. Setting the mouse position on the screen

SetCursorPos(x, y);

This function lets the virtual pointer to come at the specified co-ordinates (X,Y). You can also find the current pointer location using System.Windows.Forms.Control.MousePosition attribute.

3. Performing the mouse click event

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);

The above two steps performs mouse down and mouse up function which eventually performs a single click of the mouse.

Creating KeyBoard Events


To perform a Keyboard event, we will use a pre-made dll avaliable to us which makes the code more compact and easy. Click Here to download the InputSimulator.dll...........

Example to write something automatically:

      InputSimulator.SimulateTextEntry("Hello World");

Example to simulate a Key Down:

     InputSimulator.SimulateKeyDown(VirtualKeyCode)

Simiarly you can explore many other methods exposed by InputSimulator.dll like:

* SimulateKeyUp
* SimulateKeyPress
* SimulateModifiedKeyStroke  // for Special Keys or a combination of keys

Adding Shortcuts to the Buttons on your GUI


See the below example in which we will over-ride the ProcessCmdKey method of System.Windows.Form. It will make your GUI more easy to operate

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.F1))
            {
                btnFindMouse.PerformClick();
                return true;
            }
            if (keyData == (Keys.F2))
            {
                btnSaveStep.PerformClick();
                return true;
            }
            if (keyData == (Keys.F3))
            {
                btnStartRobot.PerformClick();
                return true;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

Comments

viji said…
you are posting a good information for people and keep maintain and give more updates too.

seo company india
Sumathi said…
I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also informative. I hope to read more and more interesting articles from your blog. All the best.
Dandruff Shampoo
Unknown said…
Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much.
Psoriasis Treatment
Pimple Treatment
Nicole Bolton said…
There are many interesting information included and i can easily understand all given information.I post something on my blog to post something, or wait to post something worth saying. Keep update more information....
Logistics management software
Manufacturing ERP
Oil and gas ERP
Unknown said…
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us



Best Training Institute in chennai
Ancy merina said…
This comment has been removed by the author.
Unknown said…
Wonderful blog....Such a very informative and creative contents
Python course in chennai


Unknown said…
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....

Carwash in omr
usedcars in omr
automotors in omr
car accessories in omr

Popular posts from this blog

CDAC CCAT Rank - Which Centre you Should go for...

Subscribe us for our YouTube channel and any kind of help Click here to ask questions regarding CDAC 1. C-DAC (Head Quarters) Pune    CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC                         click below to know about the CCAT's This batch allotment                click above to know about the CCAT's This batch allotment Rank 1-300 c-dac HQ has been the best from the start. 2.  Sunbeam Pune Rank 300-500 I got very positive feedback from my friends who are in c-dac banglure main campus,since there are many companies you may get more opportunities. 3.  C-DAC Knowledge Park Rank 400-700 It as very good faculty .Almost all the students get placed here every year. 4. C-DAC Hyderabad Rank 200-1000 (It depends on the course which you select) C-DAC hyderabad is very good for the course PG-DESD. PG-DESD course is in hyderbad is better than pune HQ as per the past feedback.It as v

CDAC COURSES AND PLACEMENTS, WHICH IS BETTER FOR YOU

Subscribe us for our YouTube channel and any kind of help   Click here to ask questions regarding CDAC Before going through this post, I would like to draw your attention towards the importance of this post. This page not only explains my experience in CDAC but also aims at answering the queries of you all who are going or looking to have a course from CDAC. Kindly post your queries at the bottom of this page and we will get back to you within 24 hours. Kindly do not post your queries as an Anonymous user and do not forget to subscribe via email so as to keep track of your query.                         click below to know about the CCAT's This batch allotment                 click below to know about the CCAT's This batch allotment ______________________________________________ NOW a day lot of the graduates and post graduates are wondering most of the times on which course they should go for. What are the pros & cons of joining a particular ce

Placement Statistics - 2011 & 2012

Subscribe us for our YouTube channel and any kind of help                          Post your queries below and we will get back to you in no more than 24 hours.  Click here to ask questions regarding CDAC                         click below to know about the CCAT's This batch allotment                     click above to know about the CCAT's This batch allotment CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC Click here to see placement statistics About C-CAT, Exam Pattern and Books No. of Seats Across Various Training Centres Important Dates - 2014 Tags: CDAC, CDAC scope in future, CDAC placements, CDAC training, CDAC recruitment, CDAC training centres, DSSD, DESD, DAC, PGDSSD, PGDESD, PGDAC, DABC, PGDABC, VLSI, PGDITISS, PGDIVESD, PGDESD, PGDWiMC, placement statistics