Using a system hotkey in a Visual C # program

Source: Internet
Author: User
Tags bool

1. First introduce System.Runtime.InteropServices

using System.Runtime.InteropServices;

2. Declare two API functions inside the class, and their positions are equivalent to the member variables of the class.

[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);

3. Define a Keymodifiers enumeration so that the key combinations appear

public enum KeyModifiers
{
 None = 0,
 Alt = 1,
 Control = 2,
 Shift = 4,
 Windows = 8
}

4. In class constructor out registry system hotkey

example, the following example registers four hot keys:

public MainForm()
{
 InitializeComponent();
 RegisterHotKey(Handle, 100, 2, Keys.Left); // 热键一:Control +光标左箭头
 RegisterHotKey(Handle, 200, 2, Keys.Right); / /热键一:Control +光标右箭头
 RegisterHotKey(Handle, 300, 2, Keys.Up); // 热键一:Control +光标上箭头
 RegisterHotKey(Handle, 400, 2, Keys.Down); // 热键一:Control +光标下箭头
 ....;
}

5. Rewrite the WndProc () method to invoke the procedure by monitoring system messages

Example:

protected override void WndProc(ref Message m)//监视Windows消息
{
 const int WM_HOTKEY = 0x0312; //如果m.Msg的值为0x0312那么表示用户按下了热键
 switch (m.Msg)
 {
  case WM_HOTKEY:
  ProcessHotkey(m); //按下热键时调用ProcessHotkey()函数
  break;
 }
 base.WndProc(ref m); //将系统消息传递自父类的WndProc
}

5. Needless to say, we will then need to implement the Processhotkey function:

//按下设定的键时调用该函数
private void ProcessHotkey(Message m)
{
 IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
 //MessageBox.Show(id.ToString());
 string sid = id.ToString();
 switch (sid)
 {
  case "100": DecreseVolumnb(); break; // 按下Control +光标左箭头,调用函数DecreseVolumnb();
  case "200": AddVolumnb(); break; // 按下Control +光标右箭头,调用函数AddVolumnb()
  case "300":// 按下Control +光标上箭头,显示窗体
   this.Visible = true;
   break;
  case "400":// 按下Control +光标下箭头,隐藏窗体
   this.Visible = false;
   break;
 }
}

It is obvious that the following function DECRESEVOLUMNB () is implemented separately; and ADDVOLUMNB (); Can.

6. Finally, don't forget to cancel the registration of the hotkey when the program exits

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
 UnregisterHotKey(Handle, 100); //卸载第1个快捷键
 UnregisterHotKey(Handle, 200); //缷载第2个快捷键
 UnregisterHotKey(Handle, 300); //卸载第3个快捷键
 UnregisterHotKey(Handle, 400); //缷载第4个快捷键
}

The above is the whole process of using a system hotkey in a C # program.

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.