2012-12-14 125 views
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指出,

+1

此热键只在您的应用程序中运行,还是需要在系统范围内运行? –

+0

它需要是一个系统范围的。 –

+0

我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

回答

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])); 
} 
+0

在我发给你的代码中使用全局Atom的原因是,可以检查热键(动作)是否已经注册。你的代码会导致异常。 –

+0

是的,你是正确的这确实会导致异常,但我已经解决了这个问题。我从来没有回来更新这个。为了避免异常,我真的必须将RegisterHotKey分配给一个布尔值,然后检查布尔值 –

2

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

http://sdrv.ms/Wc2R5H

+0

技术上在VB中,但足够接近给我一个出发点。谢谢。 –

+1

不客气。重要的位在Win32.vb中。其余的只是一个组件实现。应该很容易适应你的目的。或者按原样编译项目并使用它。我免费给社区。 –

+0

我已经修改它以符合我的需求。 –