tags:

views:

821

answers:

4

I am trying to create an automated clicker program in C# to make some administrative tasks quicker. In short, I will be using one program (Firefox) and will need a program to automatically move the mouse and click wherever I tell it to.

At the moment I have this code, although I think I'm pretty far away from an actual solution.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Clicker
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        public void DoMouseClick()
        {
            //Call the imported function with the cursor's current position
            Cursor.Position = new Point((int)10, (int)10);

            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
        }

        public Form1()
        {
            //InitializeComponent();
        }
    }
}

I want my program to run on top of Firefox and once it has loaded to immediately start clicking in the positions I tell it to. It'll need to click thirteen 30x30 pixel buttons and will then stop, leaving me to finish the task.

How would I go about such a task?

EDIT: I forgot to mention that the front-end is in Flash. Weird, I know. Either way, it means that programs like Selenium won't work as its tracks page movements and clicks on links.

+2  A: 

While this doesn't answer your question with C# code I would really have to recommend you look at AutoIt. It is a very simple scripting language for creating defined tasks to be repeated. I've used it successfully for a wide range of tasks from repeatedly entering in form data for testing web applications to fighting monsters in a MMO game.

Chris Marisic
Worked perfectly. Thank you!
EnderMB
+2  A: 

Does it need to actually automate the mouse, or just press the buttons? You could easily do this with Selenium, including recording your actions with the IDE (a FireFox addin) which produces the C# code for you.

Steven Robbins
I've tried Selenium, but it doesn't work for Flash applications. I forget to mention that the part I need to automate has a Flash front-end, something that Selenium doesn't pick up on.
EnderMB
There's community versions like http://code.google.com/p/flash-selenium/ that help fill that gap, although I can't say I've used them.
Steven Robbins
+1  A: 

To expand on @Chris Marisic's answer, you can actually use AutoIt from C# (or other languages) using AutoItX. I haven't tried this, but apparently you can compile it to a DLL and call it from your code, see http://www.autoitscript.com/forum/index.php?showtopic=39262

Adam Neal
Awesomeness that I didn't know existed
Chris Marisic
A: 

Here's a simple library that is pretty nifty:

http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx

Chris Dunaway