Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2010 C#5
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:
if(moveCursor)
{ 
   //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:http://www.autohotkey.com/docs/commands/Click.htm[^]) but didn't quite get it.
 
Thanks for Your help!
Posted 12-Jun-11 9:08am
Edited 13-Jun-11 0:17am
v3
Comments
SAKryukov at 12-Jun-11 14:18pm
   
Where did you get C# v.5. I want it, too! :-)
--SA
AmarjeetAlien at 12-Jun-11 14:20pm
   
That only Codeproject knows!! I saw that later.Basically I hit uparrow+enter :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A global hook only helps if you need to consume all available event. You can do it using P/Invoke and Windows Hooks. See http://msdn.microsoft.com/en-us/library/ms632589(v=vs.85).aspx[^].
 
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 http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx[^].
 
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 http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmouseclick.aspx[^].
 
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++) {
            this.OnMouseClick(eventArgs);
            System.Threading.Thread.Sleep(clickDelay);
        } //loop click
        //another way, depending on what did you handle:
        for (int index = 0; index < count; index++) {
            this.OnMouseDown(eventArgs);
            System.Threading.Thread.Sleep(downDelay);
            this.OnMouseUp(eventArgs);
            System.Threading.Thread.Sleep(clickDelay);
        } //loop down-up
        //maybe even this way, depending on what did you handle:
        for (int index = 0; index < count; index++) {
            this.OnMouseDown(eventArgs);
            this.OnMouseClick(eventArgs);
            System.Threading.Thread.Sleep(downDelay);
            this.OnMouseUp(eventArgs);
            System.Threading.Thread.Sleep(clickDelay);
        }  //loop down-click-up
    } //SimulateClicks

} //class MyClickingControl

[END EDIT]
 
—SA
  Permalink  
v5
Comments
AmarjeetAlien at 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:http://www.codeproject.com/script/Answers/MemberPosts.aspx?tab=questions&mid=7063045 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^...so I've to trick it!!
 
I thik this explains my problem.
SAKryukov at 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,
--SA
Abhinav S at 13-Jun-11 0:27am
   
Good answer. My 5.
SAKryukov at 13-Jun-11 0:59am
   
Thank you, Abhinav.
--SA
AmarjeetAlien at 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!
SAKryukov at 13-Jun-11 1:44am
   
It looks like my last advice Control.InvokeOnClick will do the trick.
In worst case you will need to P/Invoke "Send Inpit". I recently did it, not too hard as well.
 
Please let me know if it works. You certainly screw up the UI already, but I understand if you need a hack of the demo urgently.
--SA
AmarjeetAlien at 13-Jun-11 2:06am
   
Can you please give sample code snippet for that^^
I found this for Control.InvokeOnClick:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick.aspx
protected void InvokeOnClick(
Control toInvoke,
EventArgs e
)
 
And toInvoke type list is long:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx
I need to know which one to use.
SAKryukov at 13-Jun-11 3:16am
   
Oh! Sorry, I confused you! This event does not have coordinate of the mouse. It only triggers a general click, not mouse event. What did you use in first place? Mouse click event?
 
You need http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmouseclick.aspx
 
--SA
SAKryukov at 13-Jun-11 3:18am
   
I updated the solution again.
--SA
AmarjeetAlien at 13-Jun-11 4:39am
   
I think I'm still not clear to You. "There is no clicking and so I don't think I can use OnMouseClick".
And I think I need this:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseclick.aspx
And
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click.aspx
 
Anyway it's freaking me out!How difficult it could be to perform a simple right click at current cursor location??! Forget everything just write a simple C# program which when run performs a right click!Like have a "for" loop from 1-10 and for each even value of i(checked after a delay) do a right click.
 
I know I'm very bad at programming.But that's how learning begins.
SAKryukov at 13-Jun-11 12:00pm
   
Oh (sigh)... see another update.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In addition to the good provided earlier, this http://msdn.microsoft.com/en-us/library/ms171548.aspx[^] might help as an add-on.
  Permalink  
Comments
AmarjeetAlien at 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:http://cameramouse.org/
 
Thanks!

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 13 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100