I've tried this in the past, but never got it to work.

How would I set specific keys to run functions, whether I am on the current form or not?


Thanks!

Could you help me use this to fire an event when Mouse1 (Left button) is clicked?

Thank you!

That is a pretty good article adatapost, but keep in mind. The problem with "registering" a hot key using those functions, is that there is only 1 windows message for a hotkey that is passed to your application. that is to say if you register 10 different hotkeys, they will all fire the same function. so you must them get event data to determine the pressed key, and handle it accordingly. so its a bit more complicated that that article intails. it also doesn't go into detail about catching the messages. a simple solution would be just to override WndProc and check for the hotkey message, then if so get the key pressed with

Keys key = (Keys)(((int)message.LParam >> 16) & 0xFFFF);
                    KeyModifiers modifier = (KeyModifiers)((int)message.LParam & 0xFFFF);
//where keymodifiers is an enum of modifier keys

then compare it to keys you have set most likely in a custom collection.

A good practice if you wish to put all the code in a separate class would be to create a class that inherits from NativeWindow, it accepts a property of the form handle that it should regulate, then you can override the wndproc method there, cleanly and out of the way. you can also create a custom event args class that contains values for keys and modifiers, and create a custom event called hotkeypressed or something like that, and have it accepts your custom event args. have a class that registers your hotkeys, and creates your native window class instance, and hold the event. then you can create a HOTKEY class, with like 3 properties, the key, the modifier enum value, and the method to be executed.

Ok now you create your hotkey class passing to it your form handle, and you create a eventhandler method for you custom event. then in the handler you you get the values from you eventargs and loop through a collection of your hotkey class and compare, when you have a match. you call the function.

I KNOW!!!! it sounds like a lot. and it really is. but the result is a very modular, easy to reuse code for using Multiple hotkeys.

that's how I do it atleast

Comments

Could you help me use this to fire an event when Mouse1 (Left button) is clicked?

Thank you!

here is the code and the project is attached to this post :

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

namespace CaptureMouseClicks
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_MouseClick(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
				MessageBox.Show("left button is clicked.");
			else if (e.Button == MouseButtons.Right)
				MessageBox.Show("Right button is clicked.");
			else if (e.Button == MouseButtons.Middle)
				MessageBox.Show("Middle button is clicked.");
		}
	}
}

Form1.Designer.cs :

namespace CaptureMouseClicks
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (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.SuspendLayout();
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Name = "Form1";
			this.Text = "Form1";
			this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
			this.ResumeLayout(false);

		}

		#endregion
	}
}

I don't think this is what I'm looking for, sorry.

I'd like it so when I click the left mouse button on any window, it fires an event.

Sorry for being inspecific and thank you for your help!

you just repeated my link, i think you post it without reading mine :)

lol nope, if you look we posted them at almost the exact same time, yours just went through first ;) too bad you beat me to it.

This question has already been answered. Start a new discussion instead.