C# 系統熱鍵

來源:互聯網
上載者:User

C#中沒有設定系統熱鍵的方法,需要調用系統API來實現。

在網上找了段代碼,自己實踐了一下很好用,記錄下來以方便以後使用。

HotKey類代碼如下:

using System;using System.Collections.Generic;using System.Text;using System.Collections;using System.Windows.Forms;using System.Runtime.InteropServices;public class Hotkey : IMessageFilter{    public delegate void HotkeyEventHandler(int HotKeyID);    public event HotkeyEventHandler OnHotkey;    private Hashtable keyIDs = new Hashtable();    private IntPtr hWnd;    /// <summary>    /// 輔助按鍵    /// </summary>    public enum KeyFlags    {        MOD_NULL = 0x0,        MOD_ALT = 0x1,        MOD_CONTROL = 0x2,        MOD_SHIFT = 0x4,        MOD_WIN = 0x8    }    /// <summary>    /// 註冊熱鍵API    /// </summary>    [DllImport("user32.dll")]    public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);    /// <summary>    /// 登出熱鍵API    /// </summary>    [DllImport("user32.dll")]    public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);    /// <summary>    /// 全域原子表添加原子    /// </summary>    [DllImport("kernel32.dll")]    public static extern UInt32 GlobalAddAtom(String lpString);    /// <summary>    /// 全域原子表刪除原子    /// </summary>    [DllImport("kernel32.dll")]    public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);    /// <summary>    /// 建構函式    /// </summary>    /// <param name="hWnd">當前控制代碼</param>    public Hotkey(IntPtr hWnd)    {        this.hWnd = hWnd;        Application.AddMessageFilter(this);    }    /// <summary>    /// 註冊熱鍵    /// </summary>    public int RegisterHotkey(Keys Key, KeyFlags keyflags)    {        UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());        RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);        keyIDs.Add(hotkeyid, hotkeyid);        return (int)hotkeyid;    }    /// <summary>    /// 登出所有熱鍵    /// </summary>    public void UnregisterHotkeys()    {        Application.RemoveMessageFilter(this);        foreach (UInt32 key in keyIDs.Values)        {            UnregisterHotKey(hWnd, key);            GlobalDeleteAtom(key);        }    }    /// <summary>    /// 訊息篩選    /// </summary>    public bool PreFilterMessage(ref Message m)    {        if (m.Msg == 0x312)        {            if (OnHotkey != null)            {                foreach (UInt32 key in keyIDs.Values)                {                    if ((UInt32)m.WParam == key)                    {                        OnHotkey((int)m.WParam);                        return true;                    }                }            }        }        return false;    }}

調用方法:

private Hotkey hotkey;private int hotKey_Ctrl_F2;private void btnHotKey_Click(object sender, EventArgs e){if (btnHotKey.Text == "註冊熱鍵"){hotkey = new Hotkey(this.Handle);//定義熱鍵(Ctrl + F2)hotKey_Ctrl_F2 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F2, Hotkey.KeyFlags.MOD_CONTROL);hotkey.OnHotkey += new Hotkey.HotkeyEventHandler(OnHotkey);btnHotKey.Text = "登出熱鍵";}else{hotkey.UnregisterHotkeys();btnHotKey.Text = "註冊熱鍵";}}private void OnHotkey(int HotkeyID){if (HotkeyID == hotKey_Ctrl_F2){this.WindowState = FormWindowState.Normal;this.Focus();MessageBox.Show("Ctrl+F2");}}

執行個體下載:http://files.cnblogs.com/zjfree/HotKey.rar

運行環境:WIN2003 + VS2005

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.