いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

ホットキー(HotKey)の設定 (DllImport, InteropServices, RegisterHotKey, UnRegisterHotKey)

ホットキーの設定は、.NETクラスライブラリ内では処理できないので、
Win-APIを利用します。


使用する関数は、

RegisterHotKey   => HotKey登録
UnRegisterHotKey => HotKey解除

となります。


上記api関数は、user32.dll内にて定義されているので
まず、それをインポートする必要があります。
インポートするには、

using System.Runtime.InteropServices;

が必要になります。


その後、

        [DllImport("user32.dll")]
        extern static int RegisterHotKey(IntPtr hWnd, int id, int modKey, int key);

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

として、関数をこちら側に引き込みます。
後は、使うだけです。


上記の関数の第一引数には、どちらもハンドルを渡す必要がありますが
それは、FormクラスのHandleプロパティから取得できます。


以下サンプルです。

// vim:set ts=4 sw=4 et ws is nowrap ft=cs:

using System;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace Gsf.Samples.WinForms{

    public class HotKeySample : Form{
        //
        // モディファイアキー
        //
        const int MOD_ALT     = 0x0001;
        const int MOD_CONTROL = 0x0002;
        const int MOD_SHIFT   = 0x0004;
        //
        // HotKeyのイベントを示すメッセージID
        //
        const int WM_HOTKEY   = 0x0312;
        //
        // HotKey登録の際に指定するID
        // 解除の際や、メッセージ処理を行う際に識別する値となります。
        //
        // 0x0000〜0xbfff 内の適当な値を指定.
        //
        const int HOTKEY_ID   = 0x0001;  
                                         
        //
        // HotKey登録関数
        //
        // 登録に失敗(他のアプリが使用中)の場合は、0が返却されます。
        //
        [DllImport("user32.dll")]
        extern static int RegisterHotKey(IntPtr hWnd, int id, int modKey, int key) ;

        //
        // HotKey解除関数
        //
        // 解除に失敗した場合は、0が返却されます。
        //
        [DllImport("user32.dll")]
        extern static int UnregisterHotKey(IntPtr HWnd, int ID) ;

        public HotKeySample(){
            InitializeComponent();
        }

        protected void InitializeComponent(){
            
            Text = "ALT + CTRL + SPACEにHotKey設定中・・・";

            Load += delegate(object sender, EventArgs e){
                //
                // 登録.
                //
                // ALT + CTRL + SPACEにホットキーを設定.
                //
                if(RegisterHotKey(Handle, HOTKEY_ID, MOD_ALT | MOD_CONTROL, (int) Keys.Space) == 0){
                    MessageBox.Show("既に他のアプリで使用されています。");
                }
            };

            Closed += delegate(object sender, EventArgs e){
                //
                // 解除
                //
                UnregisterHotKey(Handle, HOTKEY_ID);
            };

            Visible       = true;
            ShowInTaskbar = true;
            Size          = new System.Drawing.Size(400, 100);
        }

        /// <summary>
        /// Windowsメッセージを処理します。
        /// </summary>
        protected override void WndProc(ref Message message){

            base.WndProc(ref message);

            if(message.Msg == WM_HOTKEY){
                if(((int) message.WParam) == HOTKEY_ID){
                    //
                    // 表示・非表示を切り替え
                    //
                    Visible = !Visible;

                }
            }
        }
        
        [STAThread]
        static void Main(){
            Application.Run(new HotKeySample());
        }
    }
}


以下のリソースを参照させていただきました。感謝! m(_ _)m