Click here to Skip to main content
15,662,459 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
Hi Coders!

I'm trying to simulate a system-wide mouse event from my Windows Form Application. I haven't implemented any "global mouse hook" or "mouse event handler" in my code.

What I've is certain flags which when set triggers a system-wide mouse event. Like I've a flag
bool moveCursor = false;

which is set depending on a particular event in the Form(like click a button) and
(X,Y) values are passed by the application as:
   //code to get X Y values
   Cursor.Position = new Point(cursorpos.X, cursorpos.Y);

Now I need to do the same with right/left/double-click as well,i.e., use a flag which when set performs a right/left/double click at current location of the cursor.

I read about "Click" and "MouseClick" commands(here:[^]) but didn't quite get it.

Thanks for Your help!
Updated 12-Jun-11 23:17pm
Sergey Alexandrovich Kryukov 12-Jun-11 14:18pm    
Where did you get C# v.5. I want it, too! :-)
AmarjeetAlien 12-Jun-11 14:20pm    
That only Codeproject knows!! I saw that later.Basically I hit uparrow+enter :)

A global hook only helps if you need to consume all available event. You can do it using P/Invoke and Windows Hooks. See[^].

If you want to write a system-global hook, you need to write a native DLL which installs the hook. I suggest you create C++ DLL or a mixed-mode DLL with C++/CLI.

However, the hooks would not help you if you want to trigger mouse inputs event. You need to simulate mouse input, use Windows API SendInput, see[^].

I must warn against using simulation of input in development of UI. The UI should not use such tricks: all functionality can be done using just .NET library. If you explained your ultimate goal and requirements, you could get a better advice in this direction.

Simulation of input should be used only if your really need "system" tricks in your application. Examples include running of keyboard and/or mouse macro, virtual keyboard and the like.

[EDIT — based on comment by OP]
I still don't understand how exactly simulation of the click can help to deal with blink. Probably you simply want to quickly re-use some code created previously without clear plan on what would be next. It looks like you're doing a momolythic application and have access to all your control. I think you don't need any hooks or even low-level simulation at all.
Most likely, a simple call to Control.OnMouseClick will do the trick. It will work if you just need to trigger the event. See[^].

As this method is protected, you need to call it in derived control (sorry, I don't know what control type do you use).

You can simulate just the click or with separate MouseUp and MouseDown. It depends on which event do you handle. Something like this:

class MyClickingControl : Panel { //or whatever it is: Form?


    void SimulateClicks(int count, Point where, int clickDelay, int downDelay) {
        MouseEventArgs eventArgs =
            new MouseEventArgs(MouseButtons.Left, 1, where.X, where.Y, 0);
        for (int index = 0; index < count; index++) {
        } //loop click
        //another way, depending on what did you handle:
        for (int index = 0; index < count; index++) {
        } //loop down-up
        //maybe even this way, depending on what did you handle:
        for (int index = 0; index < count; index++) {
        }  //loop down-click-up
    } //SimulateClicks

} //class MyClickingControl


Share this answer
AmarjeetAlien 12-Jun-11 15:03pm    
Well,I'm not skilled enough to create ".dll" and so not in a mood to implement "Global Mouse Hook".

To know what exactly I'm trying to do you may just look at headings of my previous posts: which were ofcourse answered by you only(--SA)!

But,I'll give a small summary.

I've a loop that keeps generating X and Y value dynamically using image processing(OpenCV) by detecting the Iris of the user.
Next I need to move cursor in sync with the Iris movement and I'm passing the pupil-coordinates as corsorpostion. I've somehow finished that.

Next I need to detect blinking to perform mouse clicks(lefteye blink=left click like that) and there are no good methods available for that.So,I'm tweaking the pupildetection procedure.When the user blinks, the pupil-coordinates remain fixed to its previous value, if it remains fixed for few seconds that means user is trying to click...for normal blink rate has to be considered. That is moment when I've to trigger the clicking at current cursor location.

I got less that 24hrs to finish that^ I've to trick it!!

I thik this explains my problem.
Sergey Alexandrovich Kryukov 12-Jun-11 23:46pm    
If sound like a challenging problem, and 24 hours like a crazy deadline. I don't know how much work has left though. Maybe I wasn't clear: I mentioned "hook" only because you did and did not mean you really need to use it. Just the opposite, I tried to say that the hook cannot simulate input. I just tried to lay out more or less complete information.

At the same time, I assumed you really want to simulate input but at the end expressed a doubt you really need. Now I think you don't need it, but probably your algorithm already works with click, so you simply don't have time to abstract our a real computation be separating event from it.

Unfortunately, I still did not get the idea, because you did not explain why the user clicks mouse in relation to blinking in first place. However, I got an impression that you simply need to trigger a mouse event which is already handled. So, I added a method you would call do trigger it.

Please see if it can work (my updated answer, under [EDIT]).
Good luck,
Abhinav S 13-Jun-11 0:27am    
Good answer. My 5.
Sergey Alexandrovich Kryukov 13-Jun-11 0:59am    
Thank you, Abhinav.
AmarjeetAlien 13-Jun-11 1:25am    
Sorry for my "small" summary.

Actually, I'm making an iMouse(eye mouse) where user uses my hardware setup(a headmount camera) to interact with computer.The user is a limbic disable(no hands) and can't use the normal mouse. My application software is to be used like a driver(not actually a driver) which detects headmount camera and allows the user to interact with computer by tracking his/her eyeball movements and blinking.

What I should have made clear is that user is "not clicking". He/She is just blinking the eye(s) and my app has to detect the blink and perform the clicking at current cursor location.The blink to perform a click is slightly longer than normal blink.

So, there are no mouse event from user side(as is the case with all available code samples) and so I can't use "OnClick" type events handlers :(

I can detect blink and all I need is to trigger the click!

Moreover I'm a Student and not a professional and so I'm not dragging myself into std GUI practices. I only need to give a demo(in fact, final presentation) that it is possible to click using blink.

Thanks for your help --SA!
In addition to the good provided earlier, this[^] might help as an add-on.
Share this answer
AmarjeetAlien 13-Jun-11 1:34am    
I've been through this page a number of times, but problem remains same->the user has to perform a mouse/keyboard event in this example.My user is using "onscreen keyboard" and no mouse at all! Please read my above comment.
Something like this:


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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900