Click here to Skip to main content
15,881,742 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i've created a console application that does one click after it started. Here is the code:

C#
class Program
{
    static void Main(string[] args)
    {

        Thread.Sleep(5000);
        mouse_event((uint)MouseEventFlags.LEFTDOWN, 100, 100, 0, 0);
        Thread.Sleep(300);
        mouse_event((uint)MouseEventFlags.LEFTUP, 100, 100, 0, 0);
    }

    [Flags]
    public enum MouseEventFlags
    {
        LEFTDOWN = 0x00000002,
        LEFTUP = 0x00000004,
        MIDDLEDOWN = 0x00000020,
        MIDDLEUP = 0x00000040,
        MOVE = 0x00000001,
        ABSOLUTE = 0x00008000,
        RIGHTDOWN = 0x00000008,
        RIGHTUP = 0x00000010
    }

    [DllImport("user32.dll")]
    private static extern void mouse_event(
        long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);


}


This sample works in notepad, for example, but in other application it does nothing. An application in which it doesn't work is the clicker mentioned below.

what do i have to do to make it work in other applications?

Here is a clicker i found online Clicker(take the first one), and this one can click on another applications that my program doesn't click. You can run my code in this window clicker and it doesn't work, it would be great if my clicker can click start/stop in this clicker.

Best regards!
Posted
Comments
Sergey Alexandrovich Kryukov 6-Apr-15 18:12pm    
Why, why? Yet another attempt to abuse technology, it looks like, to me...
—SA
Gabriel Sas 7-Apr-15 5:09am    
i want to automate some clicks that can be done by me for a couple of seconds, clicking objects and moving them.
I also can't use the one on the internet because i want to also do some image processing and based on some values in the image to click some positions or not.

I think its not an abuse to the technology, just doing some stuff others missed out in doing it right.
Sergey Alexandrovich Kryukov 7-Apr-15 8:51am    
I think it might be certain abuse. No, it can be useful as an exercise, but think about it: you have some other application and try to "automate" it. But it wasn't designed for such "automation". In other words, instead of developing some system for productivity which replaces "bad" poorly productive applications, you try to reuse them.

Don't consider my words as something negative. First, I don't say I'm 100% sure about your particular case. Frankly, I've done something like that myself. I just don't think it can make a useful product...

—SA

1 solution

As mouse_event is obsolete, it's better to use Windows API SendInput:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx,
and this is P/Invoke for it: http://www.pinvoke.net/default.aspx/user32.sendinput.

As this is a very low-level input simulation, it has the same effect as real hardware mouse or keyboard, so it's very reliable. Because of this low level, it does not matter what are the application; this is the same as clicking by a real mouse. I never faced any problems with it. The only question would be "why"?

—SA
 
Share this answer
 
Comments
Gabriel Sas 7-Apr-15 6:42am    
still not working with SendInput :|
Sergey Alexandrovich Kryukov 7-Apr-15 8:45am    
You can screw up anything; how do you know where? The problem is not in SendInput.
—SA
Gabriel Sas 7-Apr-15 11:34am    
i google a little and found out that when using sendinput i have to deploy the application on x86, by default my visual studio does x64, and this was the solution for them.
I'll check it out when i get home.
Sergey Alexandrovich Kryukov 7-Apr-15 11:55am    
Oh gosh, with the same success you could install code for ARM on x86-64, IE64 or x86... :-)
Hope now you code will work...
—SA

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