我正在尝试在 Visual c# 2012 中注册一个全局热键,构建目标框架 .NET3,在使用http://www.dreamincode.net/forums/topic/180436-global-hotkeys/作为教程之后,我得到了以下(缩写)文件:
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); } }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; } }我的 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/的热键资源管理器来检查热键是否已被使用,但它只告诉我它是免费的。
我能做些什么来解决这个问题?