2

i already make a simple program to recording mouse position and playback. Now i wanna add event if the mouse left click and right mouse click. But i still not understand how to do it. I already try code from many site, but still not work. Any one wanna help me please ? I'm still learning about programing, i just wanna make simple program.

this is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoClicker
{
 public partial class Form1 : Form
{
    ListViewItem lv;

    int a, b;

    public Form1()
    {
        InitializeComponent();
        this.Closing += new System.ComponentModel.CancelEventHandler(this.FormClosingEventCancle_Closing); //Menangkap event x di klik
    }


    private void FormClosingEventCancle_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        DialogResult dr = MessageBox.Show("Yakin ingin keluar?", "Konfirmasi", MessageBoxButtons.YesNo); if (dr == DialogResult.No)
            e.Cancel = true;
        else
            e.Cancel = false;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
        btn_putar.Enabled = false;
        btn_rekam.Enabled = false;
        btn_berhenti.Enabled = true;
    }

    private void timer2_Tick(object sender, EventArgs e)
    {
        //set posisi baru mouse
        if (a != b)
        {
            Cursor.Position = new Point(int.Parse(listView1.Items[a].SubItems[0].Text), int.Parse(listView1.Items[a].SubItems[1].Text));
            a++;
        }
            //agar bisa rekam ulang dan data di set ulang
        else
        {
            btn_rekam.Enabled = true;
            btn_putar.Enabled = false;
            btn_berhenti.Enabled = false;
            listView1.Clear();
            a = 0;
            b = 0;
            timer2.Stop();
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        lv = new ListViewItem(Cursor.Position.X.ToString());
        lv.SubItems.Add(Cursor.Position.Y.ToString());           
        listView1.Items.Add(lv);

        b++;
    }

    private void btn_berhenti_Click(object sender, EventArgs e)
    {
        btn_rekam.Enabled = true;
        btn_putar.Enabled = true;
        timer1.Stop();
        timer2.Stop();
    }

    private void btn_putar_Click(object sender, EventArgs e)
    {
        timer2.Start();
        btn_putar.Enabled = false;
        btn_rekam.Enabled = false;
        btn_berhenti.Enabled = false;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        a = 0;
        b = 0;
        btn_berhenti.Enabled = false;
        btn_putar.Enabled = false;

    }
  }
}
Hermanto
  • 23
  • 1
  • 4

2 Answers2

1

You can use some of Mouse events like MouseClick, MouseDown, MouseUp, etc.

For example:

    protected override void OnMouseDown(MouseEventArgs e)
    { 
        if(e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            //Do some stuff
            MessageBox.Show("Lefty!");
        }
        else if(e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            //Do some stuff
            MessageBox.Show("Righty!");
        }
    }

Assuming you copy/pase this code somewhere in Form1 class, it will override Form1's OnMouseDown event. When you left/right click on form, you'll get related MessageBox.

If you want to use the event on any other Control, you need to override that control's related event.


*Edit after comment:

    public void OnMouseDown(object sender, MouseEventArgs e)
    { 
        if(e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            //Do some stuff
            MessageBox.Show("Lefty!");
        }
        else if(e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            //Do some stuff
            MessageBox.Show("Righty!");
        }
    }

And add OnMouseDown event to any Control on form load. For example:

    private void Form1_Load(object sender, EventArgs e)
    {
        a = 0;
        b = 0;

        button1.MouseDown += OnMouseDown;
        listView1.MouseDown += OnMouseDown;
    }

That way, when you left/right click on button1 or listView1 you will get a MessageBox.

uTeisT
  • 2,256
  • 14
  • 26
  • thanks bro for your example, it's same like Cuken's sample. Still not work if out of Form1 area. – Hermanto Mar 22 '16 at 10:53
  • Hi, as I have said _"If you want to use the event on any other Control, you need to override that control's related event."_ I'll edit to match your requirement. – uTeisT Mar 22 '16 at 11:56
  • sorry bro, i'm so newbie in dekstop programming. I don't know how to set to other control. I wanna make it can record my activity at windows not just on my program's form. So how i can do it ? Do you mind give me example please ? :) actually i don't know what is the control name, so my application can record all my activity on windows – Hermanto Mar 23 '16 at 14:55
0

You can setup an event handler to fire whenever the mouse button is clicked (shown below;)

namespace MouseClickDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
        InitializeComponent();
        MouseClick += Form1_MouseClick;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Left)
        {
            //Left mouse button hit
        }
        if(e.Button == MouseButtons.Right)
        {
            //Right mouse button hit
        }
    }
}
Cuken
  • 29
  • 3
  • thanks for your example bro, it's work, but when i'm click out of Form area it's doesn't work. Do you mind tell me how to do that ? I wanna make a program for recording mouse click and position for do an autoclick. – Hermanto Mar 22 '16 at 10:50
  • @Hermanto this is because in the example of Cuken, it's only assigned to `Form1`. If you want to add it for every control you have to assign it to every control. – roemel Mar 22 '16 at 11:00
  • @Roman : i'm wanna make this program for record all my activity at Windows and then if i wanna do it again just playback. So how i assigned to all of that bro ? Thanks for your information, now i know about assignment :) If you not mind, i wanna Pm you with FB or email. I'm so new in dekstop programming. I usualy use console programming, so i need friend to share about dekstop programming :) my FB : www.facebook.com/TheH13 and my email : bellatomania@yahoo.com – Hermanto Mar 22 '16 at 11:54
  • 1
    It sounds like you're trying to record your actions on your computer and then do them again. Why don't you just use macro recorders for windows? You can just download them from google and then use them on your windows OS. Do not reinvent the wheel :) – roemel Mar 22 '16 at 12:34
  • @Roman : yeah bro you right, but i am interested try to coding :) I already have application named mouse and keyboard record, but the program it can not do the job on a scheduled basis. For example I had to repeat the activity at a particular time. So i wanna recreate my aplication :) – Hermanto Mar 23 '16 at 14:51
  • @Hermanto - The mouse click event will only fire when you're active in your form and click within your form. If you are trying to globally hook into the mouse clicks when your form is not active, you'll need to do something like [this](http://stackoverflow.com/questions/11607133/global-mouse-event-handler) – Cuken Mar 23 '16 at 15:47