1

我的解决方案

[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 指出这一点

4

2 回答 2

2

这是一个 VS2010 项目的链接。我去年写的。文件托管在 SkyDrive 上。

http://sdrv.ms/Wc2R5H

于 2012-12-14T18:59:25.990 回答
0

这是我开发的解决方案,这是一种看起来运行良好的 C# 方法。

[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]));
}
于 2012-12-17T16:13:50.200 回答