C#|グローバルホットキーを登録する

グローバルホットキーを登録して、 グローバルホットキーが押されたときにイベントを発生させるクラス。

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

/// <summary>
/// グローバルホットキーを登録するクラス。
/// 使用後は必ずDisposeすること。
/// </summary>
public class HotKey :IDisposable {
    HotKeyForm form;
    /// <summary>
    /// ホットキーが押されると発生する。
    /// </summary>
    public event EventHandler HotKeyPush;

    /// <summary>
    /// ホットキーを指定して初期化する。
    /// 使用後は必ずDisposeすること。
    /// </summary>
    /// <param name="modKey">修飾キー</param>
    /// <param name="key">キー</param>
    public HotKey(MOD_KEY modKey, Keys key) {
        form = new HotKeyForm(modKey, key, raiseHotKeyPush);
    }

    private void raiseHotKeyPush() {
        if(HotKeyPush != null) {
            HotKeyPush(this, EventArgs.Empty);
        }
    }

    public void Dispose() {
        form.Dispose();
    }

    private class HotKeyForm :Form {
        [DllImport("user32.dll")]
        extern static int RegisterHotKey(IntPtr HWnd, int ID, MOD_KEY MOD_KEY, Keys KEY);

        [DllImport("user32.dll")]
        extern static int UnregisterHotKey(IntPtr HWnd, int ID);

        const int WM_HOTKEY = 0x0312;
        int id;
        ThreadStart proc;

        public HotKeyForm(MOD_KEY modKey, Keys key, ThreadStart proc) {
            this.proc = proc;
            for(int i = 0x0000; i <= 0xbfff; i++) {
                if(RegisterHotKey(this.Handle, i, modKey, key) != 0) {
                    id = i;
                    break;
                }
            }
        }

        protected override void WndProc(ref Message m) {
            base.WndProc(ref m);

            if(m.Msg == WM_HOTKEY) {
                if((int)m.WParam == id) {
                    proc();
                }
            }
        }

        protected override void Dispose(bool disposing) {
            UnregisterHotKey(this.Handle, id);
            base.Dispose(disposing);
        }
    }
}

/// <summary>
/// HotKeyクラスの初期化時に指定する修飾キー
/// </summary>
public enum MOD_KEY :int {
    ALT = 0x0001,
    CONTROL = 0x0002,
    SHIFT = 0x0004,
}

 

以下の様に使用する。

using System;
using System.Windows.Forms;

public partial class Form1 :Form {
    HotKey hotKey;

    public Form1() {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {
        hotKey = new HotKey(MOD_KEY.ALT | MOD_KEY.CONTROL | MOD_KEY.SHIFT, Keys.F);
        hotKey.HotKeyPush += new EventHandler(hotKey_HotKeyPush);
    }

    void hotKey_HotKeyPush(object sender, EventArgs e) {
        MessageBox.Show("ホットキーが押されました。");
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
        hotKey.Dispose();
    }
}

 

One thought on “C#|グローバルホットキーを登録する

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください