[C #] using Application.addmessagefilter as a hotkey for form

Source: Internet
Author: User

Application.addmessagefilter can help you monitor all messages sent to the form by the system.
We use it to filter messages.
We can make the hotkeys we need.

This is Application.addmessagefilter's calling method.
A IMessageFilter object must be passed as a parameter

public static void Addmessagefilter (

IMessageFilter value

)

We're doing a hotkey category that inherits the IMessageFilter interface.

Class Hotkey:imessagefilter
{
...
}

Then use two attributes in the hotkey category to record the combination of hotkeys
and give the initial value when the hotkey is established.

Keys _hotkey = Keys.none;
Keys _combokey = Keys.none;
Public HotKey (keys HotKey, keys Combokey)
{
_hotkey = HotKey; Hotkey
_combokey = Combokey; Combination keys, must be set Keys.control, Keys.alt, Keys.shift, Keys.none equivalence only function
Application.addmessagefilter (this); Use the hotkey category to monitor messages
}

The IMessageFilter interface must also implement a Prefiltermessage method
To monitor/filter the messages we want
When we press the correct hotkey
It triggers a custom event Onhotkey

public delegate void Hotkeyeventhandler (object sender, EventArgs e);
public event Hotkeyeventhandler Onhotkey; Custom events
const int WM_KEYDOWN = 0x100; Press the message on the keyboard
const int wm_altkeydown = 0x104; Press the ALT key for the message
public bool Prefiltermessage (ref Message m)
{
if (Onhotkey! = null && (m.msg = = Wm_keydown | | m.msg = = wm_altkeydown))//When the keyboard is pressed
{
if ((Keys) (int) M.wparam & keys.keycode) = = _hotkey && Control.modifierkeys = _combokey)//If Hotkey + combo key matches
{
Onhotkey (this, null); Triggering custom Events
return true; and intercept this message, the form will no longer receive this message.
}
}
return false;
}

By this side, we've finished a simple version of the hotkey.
Let's test it out.
Create four sets of hotkeys in the Form1_Load

private void Form1_Load (object sender, EventArgs e)
{
HotKey HotKey = new HotKey (KEYS.A, Keys.alt); Register Alt+a as Hotkey
Hotkey. Onhotkey + = new Hotkey.hotkeyeventhandler (Hotkey_onhotkey);
HotKey Hotkey2 = new HotKey (keys.b, Keys.control); Register Ctrl+b as Hotkey
Hotkey2. Onhotkey + = new Hotkey.hotkeyeventhandler (Hotkey2_onhotkey);
HotKey Hotkey3 = new HotKey (KEYS.C, Keys.shift); Register Shift+c as Hotkey
Hotkey3. Onhotkey + = new Hotkey.hotkeyeventhandler (Hotkey3_onhotkey);
HotKey Hotkey4 = new HotKey (KEYS.F2, Keys.none); Register F2 as Hot key, if do not combine key please pass keys.none when parameter
Hotkey4. Onhotkey + = new Hotkey.hotkeyeventhandler (Hotkey4_onhotkey);
}
void Hotkey_onhotkey (object sender, EventArgs e)
{
MessageBox.Show ("Hotkey alt+a is pressed");
}
void Hotkey2_onhotkey (object sender, EventArgs e)
{
MessageBox.Show ("Hotkey ctrl+b is pressed");
}
private void Hotkey3_onhotkey (object sender, EventArgs e)
{
MessageBox.Show ("Hotkey shift+c is pressed");
}
private void Hotkey4_onhotkey (object sender, EventArgs e)
{
MessageBox.Show ("Hotkey F2 is pressed");
}

Http://www.dotblogs.com.tw/sam319/archive/2010/01/06/12876.aspx

[C #] using Application.addmessagefilter as a hotkey for form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.