0

Visual c# 2012 でグローバル ホットキーを登録しようとしていて、ターゲット フレームワーク .NET3 をビルドします省略) ファイル:

  1. GlobalHotkey.cs

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace barcodelabel
    {
        public class GlobalHotkey
        {
            private int modifier;
            private int key;
            private IntPtr hWnd;
            private int id;
    
            public GlobalHotkey(int modifier, Keys key, Form form)
            {
                this.modifier = modifier;
                this.key = (int)key;
                this.hWnd = form.Handle;
                id = this.GetHashCode();
            }
    
            public bool Register()
            {
                return RegisterHotKey(hWnd, id, modifier, key);
            }
    
            public bool Unregister()
            {
                return UnregisterHotKey(hWnd, id);
            }
    
            public override int GetHashCode()
            {
                return modifier ^ key ^ hWnd.ToInt32();
            }
    
            [DllImport("user32.dll")]
            private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
    
            [DllImport("user32.dll")]
            private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        }
    }
    
  2. GlobalHotkeyConstants.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace barcodelabel
    {
        class GlobalHotkeyConstants
        {
            public const int NOMOD = 0x0000;
            public const int ALT = 0x0001;
            public const int CTRL = 0x0002;
            public const int SHIFT = 0x0004;
            public const int WIN = 0x0008;
    
            //windows message id for hotkey
            public const int WM_HOTKEY_MSG_ID = 0x0312;
    
        }
    }
    
  3. 私の Form1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Windows;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    
    namespace barcodelabel
    {
        public partial class Form1 : Form
        {
    
            private GlobalHotkey ghk;
    
             protected override void WndProc(ref Message m)
            {
                if (m.Msg == GlobalHotkeyConstants.WM_HOTKEY_MSG_ID) {
                    MessageBox.Show("HOTKEY PRESSED");
    
                }
                base.WndProc(ref m);
            }
    
           public Form1()
           {
               InitializeComponent();
               this.ghk = new GlobalHotkey(GlobalHotkeyConstants.SHIFT,  Keys.F10, this);
           }
    
           private void Form1_Load(object sender, EventArgs e)
           {
               if (!this.ghk.Register()) 
               {
                   MessageBox.Show("Hotkey could not be registered");
               }
           }
    
           private void Form1_FormClosing(object sender, FormClosingEventArgs e)
           {
               this.ghk.Unregister();
           }
       }
    

どのホットキーを選んでも登録できません。http://hkcmdr.anymania.com/のホットキーエクスプローラーを使用して、ホットキーが既に取得されているかどうかを確認しようとしましたが、無料であるとしか言われませんでした。

これを解決するにはどうすればよいですか?

4

1 に答える 1