我的解決方案
[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/)」,其中的共識是「不,他們不應該」。 –