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

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 and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

Java Training in Chennai
Unknown said…
This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.

Fresher Jobs in Mumbai
Fresher Jobs in Pune
Fresher Jobs in Noida
Fresher Jobs in Hyderabad
Mahalya sree said…
Wow.. Thanks much for sharing.. My friend also recommended you so that i can have a helping hand to make my blog as effective as possible.
Home Interior Decorators in Chennai
Home Interior Designers in Chennai
Home Interiors in Chennai
viji said…
you are posting a good information for people and keep maintain and give more updates too.

seo company india
Hema said…
you are giving a very interesting post and it is usefull.
informatica training in chennai
Sindhuja Ravi said…
your post conveys a good messages and interesting things and give more updates.
digital marketing company in india
sivaranjani said…
Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information
SEO Company in India
Unknown said…
Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, very nice article.
SAP Training in Chennai
SAP Basis Training in Chennai
SAP SD Training in Chennai
SAP FICO Training in Chennai
subuvenni said…
i really like this blog.And i got more information's from this blog.thanks for sharing!!!!
Iperidigi
SEO Company in Chennai
SEO Company in India
Anonymous said…
this is very nice post and thanks for sharing this wonderful information to us.i really would like to appreciate to your attention for us.

Hadoop Training in Chennai
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
Raghu said…
Just read your website. Good one. I liked it. Keep going. you are a best writer your site is very useful and informative thanks for sharing!

Herbal Shampoo
Dandruff Treatment
Antifungal Cream
Vitiligo Medicines
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…
Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Android Training in Chennai
Ios Training in Chennai
Domnic said…
Anybody want to know How to trace the mobile with location and get more details for tracking.Read this blog...

Mobile Number Tracker | Phone Number Tracker With Location | Mobile Location Tracker | How To Track A Phone Number | Phone Number Tracker | Phone Number Search
The article is very different in the topic points and the way of contribution is good.

Python Training in Chennai
Unknown said…
This is so informative blog and i have used this 5 minutes thing very useful too happy with this blog........
SAP FICO Training in Chennai
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
Unknown said…


This blog was very useful for me waiting for more blog.

SAP MM Training in Chennai
Ancy merina said…
This comment has been removed by the author.
Unknown said…
Great and really helpful article! Adding to the conversation, providing more information, or expressing a new point of view...Nice information and updates. Really i like it and everyday am visiting your site..

SAP SD Training in Chennai

Unknown said…
A nice article. I totally love the way you presented the topic.Hope to see you post soon again

Hadoop Training in Chennai
Unknown said…
Content is very clear and easily explained it is very interesting as well. I hope you post again soon.

Selenium Training in Chennai
Unknown said…
Good work and its really a useful info
AWS Training in chennai
Unknown said…
Omrdeals.in is an online platform that allows people to find offers and the greatest deals in and around OMR. It is the directory of the business in and around omr. It helps internet users to surf the required deals and offers.

Offers in Chennai
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
Unknown said…
Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

Herbalife in Chennai
Wellnesscentres in Chennai
Weight loss in Chennai
Weight gain in Chennai

Popular posts from this blog

CDAC COURSES AND PLACEMENTS, WHICH IS BETTER FOR YOU

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 centre of CDAC. Here is a comprehensive information on how to enroll ourselves for a cour…

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

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 allotmentRank 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 very good placement record in hyd for PG-DESD course.Rank for PG-DESD is 1-300.
PG-DAC as no good placement record from few yea…

Placement Statistics - 2011 & 2012

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