我的解决方案
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public enum KeyModifiers : uint { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8, }
Actions<int, string> Directories = new Dictionary<int, string>();
const string MessageTitle = "Opps, Somthing Happened!";
const MessageBoxButtons msgButtons = MessageBoxButtons.OK;
const MessageBoxIcon msgIcon = MessageBoxIcon.Information;
private void btnCreateShortcut_Click(object sender, EventArgs e)
{
if (cboModifier.SelectedIndex > 0)
{
uint key = (uint)Enum.Parse(typeof(KeyModifiers), cboModifier.SelectedItem.ToString());
if (txtShortcutKey.Text != "")
CreateHotKey(key, txtShortcutKey.Text.ToString());
else
MessageBox.Show("Please enter a Hot key to use", MessageTitle, msgButtons, msgIcon);
}
else
MessageBox.Show("Please Select a Base Key", MessageTitle, msgButtons, msgIcon);
}
private void btnDestroyShortcuts_Click(object sender, EventArgs e)
{
destroyShortcuts();
}
private void quickActions_FormClosing(object sender, FormClosingEventArgs e)
{
destroyShortcuts();
}
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
case 0x0312:
if (Actions.ContainsKey((int)msg.WParam))
// Preform Action
break;
}
base.WndProc(ref msg);
}
public void destroyShortcuts()
{
foreach (KeyValuePair<int, string> pair in Actions)
UnregisterHotKey(this.Handle, pair.Key);
lstActiveKeys.Items.Clear();
Actions.Clear();
}
public void CreateHotKey(uint modifier, string key)
{
int keyID = (Actions.Count + 1) * 100;
Actions.Add(keyID, txtAction.Text.ToString());
lstActiveKeys.Items.Add(modifier + "+" + key[0] + " - " + txtAction.Text.ToString());
RegisterHotKey(this.Handle, keyID, modifier, (int)((char)key[0]));
}
我想知道如何让这个我的用户可以定义所给出的选项自己的热键选择控制键和一个字母。注册器用户自定义热键
我找到的所有代码都显示了如何定义一个热键,但一个用户可能有3个,另一个用户可能有5个,他们可能不是相同的键。
我想给我一个控制键和一个字母数字键,我可以创建一个Windows热键。
我还需要能够在应用程序关闭时销毁注册的密钥。
PS:这些需要在系统范围内,而不仅仅是在应用程序内。谢谢@ scott-chapman指出,
此热键只在您的应用程序中运行,还是需要在系统范围内运行? –
它需要是一个系统范围的。 –
我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –