Which mouse button was clicked?

   
 

using System;
using System.Windows.Forms;
using System.Drawing;

public class MainWindow : Form {
    public MainWindow() {
        Height = 300;
        Width = 500;
        this.MouseMove += new MouseEventHandler(MainForm_MouseMove);
        this.MouseUp += new MouseEventHandler(MainForm_MouseUp);
        this.KeyUp += new KeyEventHandler(MainForm_KeyUp);
    }
    protected void MainForm_MouseMove(object sender, MouseEventArgs e) {
        this.Text = string.Format("Current Pos: ({0}, {1})", e.X, e.Y);
    }

    protected void MainForm_MouseUp(object sender, MouseEventArgs e) {

        if (e.Button == MouseButtons.Left)
            MessageBox.Show("Left click!");
        if (e.Button == MouseButtons.Right)
            MessageBox.Show("Right click!");
        if (e.Button == MouseButtons.Middle)
            MessageBox.Show("Middle click!");
    }

    protected void MainForm_KeyUp(object sender, KeyEventArgs e) {
        MessageBox.Show(e.KeyCode.ToString(), "Key Pressed!");
    }
    public static void Main(string[] args) {
        Application.Run(new MainWindow());
    }
}

    


Register Form window message filter


   


  using System;
  using System.Windows.Forms;
  using Microsoft.Win32;

  public class MyMessageFilter : IMessageFilter 
  {
    public bool PreFilterMessage(ref Message m) 
    {
      // Intercept the left mouse button down message.
      if (m.Msg == 513) 
      {
        Console.WriteLine("WM_LBUTTONDOWN is: " + m.Msg);
        return true;
      }
      return false;
    }
  }

  public class mainForm : System.Windows.Forms.Form
  {
    private MyMessageFilter msgFliter = new MyMessageFilter();

    public mainForm()
    {
      GetStats();
      Application.ApplicationExit += new EventHandler(Form_OnExit);
      Application.AddMessageFilter(msgFliter);    
    }

    [STAThread]
    static void Main() 
    {
      Application.Run(new mainForm());
    }

    private void GetStats()
    {
      Console.WriteLine(Application.CompanyName+ "  Company:");
      Console.WriteLine(Application.ProductName+ " App Name:");
      Console.WriteLine(Application.StartupPath);
    }

    // Event handlers.
    private void Form_OnExit(object sender, EventArgs evArgs) 
    {
      Console.WriteLine("Exit", "This app is dead...");
      Application.RemoveMessageFilter(msgFliter);
    }
  }


           
          


Cast event sender to a control

   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class InstantiateHelloWorld
{
     public static void Main()
     {
          Form form   = new Form();
          form.Text   = "Instantiate " + form.Text;
          form.Paint += new PaintEventHandler(MyPaintHandler);
   
          Application.Run(form);
     }
     static void MyPaintHandler(object objSender, PaintEventArgs pea)
     {
          Form     form = (Form)objSender;
          Graphics graphics = pea.Graphics;
   
          graphics.DrawString("Hello from InstantiateHelloWorld!", 
                          form.Font, Brushes.Black, 0, 100);
     }
}

    


Keyboard Sample


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;

namespace KeyboardSample
{
    /// <summary>
    /// Summary description for KeyboardSample.
    /// </summary>
    public class KeyboardSample : System.Windows.Forms.Form
    {
      private System.Windows.Forms.TextBox textBox1;
      private System.Windows.Forms.TextBox textBox2;
      private System.Windows.Forms.GroupBox groupBox1;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.GroupBox groupBox2;
      private System.Windows.Forms.Label label2;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public KeyboardSample()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
         this.textBox1 = new System.Windows.Forms.TextBox();
         this.textBox2 = new System.Windows.Forms.TextBox();
         this.groupBox1 = new System.Windows.Forms.GroupBox();
         this.label1 = new System.Windows.Forms.Label();
         this.groupBox2 = new System.Windows.Forms.GroupBox();
         this.label2 = new System.Windows.Forms.Label();
         this.groupBox1.SuspendLayout();
         this.groupBox2.SuspendLayout();
         this.SuspendLayout();
         // 
         // textBox1
         // 
         this.textBox1.Location = new System.Drawing.Point(16, 24);
         this.textBox1.Name = "textBox1";
         this.textBox1.Size = new System.Drawing.Size(168, 20);
         this.textBox1.TabIndex = 5;
         this.textBox1.Text = "";
         this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
         // 
         // textBox2
         // 
         this.textBox2.Location = new System.Drawing.Point(16, 24);
         this.textBox2.Name = "textBox2";
         this.textBox2.Size = new System.Drawing.Size(168, 20);
         this.textBox2.TabIndex = 6;
         this.textBox2.Text = "";
         this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown);
         // 
         // groupBox1
         // 
         this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                this.label1,
                                                                                this.textBox1});
         this.groupBox1.Location = new System.Drawing.Point(8, 8);
         this.groupBox1.Name = "groupBox1";
         this.groupBox1.TabIndex = 7;
         this.groupBox1.TabStop = false;
         this.groupBox1.Text = "Key Monitor";
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(16, 64);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(168, 20);
         this.label1.TabIndex = 6;
         // 
         // groupBox2
         // 
         this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                this.textBox2,
                                                                                this.label2});
         this.groupBox2.Location = new System.Drawing.Point(8, 120);
         this.groupBox2.Name = "groupBox2";
         this.groupBox2.TabIndex = 8;
         this.groupBox2.TabStop = false;
         this.groupBox2.Text = "Keys Enumeration";
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(16, 64);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(168, 20);
         this.label2.TabIndex = 9;
         // 
         // KeyboardSample
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(216, 229);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.groupBox2,
                                                                      this.groupBox1});
         this.Name = "KeyboardSample";
         this.Text = "KeyboardSample";
         this.groupBox1.ResumeLayout(false);
         this.groupBox2.ResumeLayout(false);
         this.ResumeLayout(false);

      }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new KeyboardSample());
        }

      private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
      {
         label1.Text = Convert.ToString(e.KeyValue);
      }

      private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
      {
         StringBuilder sb = new StringBuilder();
         if(e.Shift) sb.Append("Shift, ");
         if(e.Alt) sb.Append("Alt, ");
         if(e.Control) sb.Append("Ctrl, ");
                    
         if(e.KeyCode==Keys.W||e.KeyCode==Keys.R||e.KeyCode==Keys.O||e.KeyCode==Keys.X)
         {
            sb.Append("Wrox Press!!");
         }
         else if(e.KeyCode==Keys.Escape&amp;&amp;e.Modifiers==(Keys.Shift|Keys.Alt))
         {
            sb.Append("Escape - that won&#039;t work!");
         }
         else if(e.KeyCode == Keys.C &amp;&amp; e.Modifiers==(Keys.Alt | Keys.Control))
         {
            sb.Append("CopyRight");
            textBox2.SelectedText = "CopyRight";
            textBox2.SelectionLength = 0;
         }
         else 
         {
            sb.Append(Convert.ToString(e.KeyData));
         }
         label2.Text = sb.ToString();
      }
    }
}


           
          


Key Timer


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace Wrox.ProgrammingWindowsGUI.Chapter5
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class KeyTimer : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private uint start = 0;
        private uint stop = 0;

        [DllImport("kernel32.dll")]
        public static extern uint GetTickCount();

        public KeyTimer()
        {
            //
            // Required for Windows Form Designer support
            //
            //InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 142);
            this.Name = "Form1";
            this.Text = "Form1";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new KeyTimer());
            
        }

        protected override void OnKeyDown(KeyEventArgs args)
        {
            start = GetTickCount();
        }

        protected override void OnKeyUp(KeyEventArgs args)
        {
            stop = GetTickCount();
            uint elapsed = (stop - start);
            MessageBox.Show(Convert.ToString(args.KeyData)+", time elapsed: "+Convert.ToString(elapsed)+" msecs");
        }
    }
}


           
          


Key Press


   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace KeyPress
{
    /// <summary>
    /// Summary description for KeyPress.
    /// </summary>
    public class KeyPress : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public KeyPress()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(184, 104);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(80, 48);
      this.button1.TabIndex = 0;
      this.button1.Text = "button1";
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(176, 192);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(80, 32);
      this.button2.TabIndex = 1;
      this.button2.Text = "button2";
      // 
      // KeyPress
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.button2,
                                                                  this.button1});
      this.Name = "KeyPress";
      this.Text = "KeyPress";
      this.Load += new System.EventHandler(this.KeyPress_Load);
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new KeyPress());
        }

    private void KeyPress_Load(object sender, System.EventArgs e)
    {
      this.KeyPreview=true;
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
      MessageBox.Show("Key Pressed = " + e.KeyChar.ToString());
    }
    }
}

           
          


Full screen and KeyEvent and MouseEvent

   

// This Article is a simple one to show the usage of KeyEvent and MouseEvent. 
  
// Please move the mouse or press a key to Exit this Program. 
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 

namespace ScreenSave 
{ 
    public class KeyEventandMouseEvent : System.Windows.Forms.Form { 
        private System.Windows.Forms.Label label1; 
        private System.Windows.Forms.Timer timer1; 
        private System.ComponentModel.IContainer components; 
        private Boolean boolFlag; 
        private int ixStart; 
        private int iyStart; 
    
        public KeyEventandMouseEvent() { 
            // 
            // Required for Windows Form Designer support 
            // 
            InitializeComponent(); 
        } 
        /// <summary> 
        /// Clean up any resources being used. 
        /// </summary> 
        protected override void Dispose( bool disposing ) 
        { 
            if( disposing ) { 
                if (components != null) 
                { 
                    components.Dispose(); 
                } 
            } 
            base.Dispose( disposing ); 
        } 
        #region Windows Form Designer generated code 
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor. 
        /// </summary> 
        private void InitializeComponent() { 
            this.components = new System.ComponentModel.Container(); 
            this.timer1 = new System.Windows.Forms.Timer(this.components); 
            this.label1 = new System.Windows.Forms.Label(); 
            this.SuspendLayout(); 
            // 
            // timer1 
            // 
            this.timer1.Enabled = true; 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 
            // 
            // label1 
            // 
            this.label1.Font = new System.Drawing.Font("Times New Roman", 14F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
            this.label1.ForeColor = System.Drawing.Color.Lime; 
            this.label1.Location = new System.Drawing.Point(8, 280); 
            this.label1.Name = "label1"; 
            this.label1.Size = new System.Drawing.Size(312, 32); 
            this.label1.TabIndex = 0; 
            this.label1.Text = "Roller - Developed in C-Sharp"; 
            this.label1.Click += new System.EventHandler(this.label1_Click); 
            // 
            // KeyEventandMouseEvent 
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
            this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64))); 
            this.ClientSize = new System.Drawing.Size(568, 32); 
            this.Controls.AddRange(new System.Windows.Forms.Control[] { 
            this.label1}); 
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
            this.KeyPreview = true; 
            this.Name = "KeyEventandMouseEvent"; 
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
            this.Text = "KeyEventandMouseEvent"; 
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized; 
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPressEvent); 
            this.Load += new System.EventHandler(this.KeyEventandMouseEvent_Load); 
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveEvent); 
            this.ResumeLayout(false); 
        } 
        #endregion 
        /// <summary> 
        /// The main entry point for the application. 
        /// </summary> 
        [STAThread] 
        static void Main() 
        { 
            Application.Run(new KeyEventandMouseEvent()); 
        } 
        private void timer1_Tick(object sender, System.EventArgs e) 
        { 
            int i; 
              
            i = label1.Location.X; 
            if (i >= 750) 
            i = 0; 
            else 
            i = label1.Location.X + 5; 
              
            label1.Location = new Point(i,280); 
        } 
        private void KeyEventandMouseEvent_Load(object sender, System.EventArgs e) 
        { 
          
        } 
        private void OnMouseMoveEvent(object sender,MouseEventArgs e) 
        { 
            //Application.Exit(); 
            if (ixStart == 0 &amp;&amp; iyStart == 0) 
            { 
                ixStart = e.X; 
                iyStart = e.Y; 
            } 
            else if (e.X != ixStart || e.Y != iyStart) 
                Application.Exit(); 
              
        } 
        private void OnKeyPressEvent(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        { 
            Application.Exit(); 
        } 
        private void label1_Click(object sender, System.EventArgs e) 
        { 
        } 
    } 
}