2012-12-14 123 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

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