//注册快捷键 public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) throw new Exception("注册失败!"); keymap[id] = callBack; }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="hWnd">持有快捷键窗口的句柄</param> /// <param name="fsModifiers">组合键</param> /// <param name="vk">快捷键的虚拟键码</param> /// <param name="callBack">回调函数</param> public static void Regist(IntPtr hWnd, HotkeyModifiers fsModifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, fsModifiers, vk)) throw new Exception("regist hotkey fail."); keymap[id] = callBack; }
/// <summary> /// 注销快捷键 /// </summary> /// <param name="hWnd">持有快捷键窗口的句柄</param> /// <param name="callBack">回调函数</param> public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap) { if (var.Value == callBack) UnregisterHotKey(hWnd, var.Key); } }
// 注销快捷键 public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { foreach (KeyValuePair <int, HotKeyCallBackHanlder> var in keymap) { if (var.Value == callBack) { UnregisterHotKey(hWnd, var.Key); return; } } }
//注册快捷键 public bool Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) { return(false); } keymap[id] = callBack; return(true); }
//注册快捷键 public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) { MessageBox.Show("快捷键Ctrl+F12已经被占用!"); return; } //throw new Exception("注册失败!"); throw会直接抛给操作系统; keymap[id] = callBack; }
//注册快捷键 public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) { //退出并输出注册失败 //throw new Exception("注册失败!"); Environment.Exit(0); } keymap[id] = callBack; }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <param name="modifiers">组合键(Ctrl,Shift,Win,Alt),多个组合键可以直接加出结果(Ctrl+Shift)</param> /// <param name="vk">按键值[a-z0-9]</param> /// <param name="callBack">回调参数</param> /// <returns></returns> public Boolean Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { Boolean is_ok = false; int id = keyid++; if (RegisterHotKey(hWnd, id, modifiers, vk)) { is_ok = true; } keymap[id] = callBack; return(is_ok); }
// 注销快捷键 public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { //foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap) //{ // if (var.Value == callBack) // { // UnregisterHotKey(hWnd, var.Key); // return; // } //} UnregisterHotKey(hWnd, 100); return; }
public static HotKey Register(Keys key, Modifiers modifiers, HotKeyCallBackHanlder callBack) { HotKey hotKey = new HotKey(key, modifiers, ++keyid); if (!WinApi.RegisterHotKey(handle, keyid, (uint)modifiers, (uint)key)) { throw new Exception("Global Hotkey Registration Failed!"); } keyMap[keyid] = callBack; return(hotKey); }
//注册快捷键 public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) { throw new Exception("注册失败!"); } hwndCallbackPair par = new hwndCallbackPair(); par.hWnd = hWnd; par.callback = callBack; keymap[id] = par; }
//注册快捷键 public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) { throw new Exception("注册失败!"); } else { ; //MessageBox.Show("注册成功!"); } keymap[id] = callBack; }
//注册快捷键 public Boolean Regist(IntPtr hWnd, Boolean key_ctrl, Boolean key_alt, Boolean key_shift, String key_other, HotKeyCallBackHanlder callBack) { string msg = ""; int modifiers = 0; if (key_ctrl) { modifiers += (int)HotKeys.HotkeyModifiers.Control; msg += "CTRL +"; } if (key_alt) { modifiers += (int)HotKeys.HotkeyModifiers.Alt; msg += "ALT +"; } if (key_shift) { modifiers += (int)HotKeys.HotkeyModifiers.Shift; msg += "SHIFT +"; } msg += key_other; int id = keyid++; Keys key; if (key_other == "←") { key = Keys.Left; } else if (key_other == "→") { key = Keys.Right; } else { key = (Keys)key_other[0]; } if (!RegisterHotKey(hWnd, id, modifiers, key)) { //throw new Exception("注册失败!"); MessageBox.Show(msg + " 快捷键注册失败。使用中的快捷键。"); return(false); } keymap[id] = callBack; return(true); }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="window">持有快捷键窗口</param> /// <param name="fsModifiers">组合键</param> /// <param name="key">快捷键</param> /// <param name="callBack">回调函数</param> public static void Regist(IntPtr hwnd, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) { var _hwndSource = HwndSource.FromHwnd(hwnd); _hwndSource.AddHook(WndProc); int id = keyid++; var vk = KeyInterop.VirtualKeyFromKey(key); if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) { throw new Exception("regist hotkey fail."); } keymap[id] = callBack; }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="hwnd">持有快捷键窗口的句柄</param> /// <param name="fsModifiers">组合键</param> /// <param name="key">快捷键</param> /// <param name="callBack">回调函数</param> public static bool TryRegist(IntPtr hwnd, HotKeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) { var _hwndSource = HwndSource.FromHwnd(hwnd); _hwndSource.AddHook(WndProc); int id = _keyId++; var vk = KeyInterop.VirtualKeyFromKey(key); if (RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) { _keyMap[id] = callBack; return(true); } return(false); }
/// <summary> /// 注销快捷键 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <param name="callBack">注册热键时的回调函数</param> /// <returns></returns> public Boolean UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { Boolean is_ok = false; foreach (KeyValuePair <int, HotKeyCallBackHanlder> var in keymap) { if (var.Value == callBack) { if (UnregisterHotKey(hWnd, var.Key)) { is_ok = true; break; } } } return(is_ok); }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="hWnd">持有快捷键窗口的句柄</param> /// <param name="modifiers">组合键</param> /// <param name="vk">快捷键的虚拟键码</param> /// <param name="callBack">回调函数</param> public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) { if (Marshal.GetLastWin32Error() == 1409) { MessageBox.Show("热键被占用 !"); } else { MessageBox.Show("注册失败!"); } } else { keymap[id] = callBack; } }
/// <summary> /// 根据键值组合字符串注册 /// </summary> /// <param name="str"></param> public bool RegistHotKeyByStr(string str, IntPtr Handle, HotKeyCallBackHanlder callback) { if (str == "") { return(false); } int modifiers = 0; Keys vk = Keys.None; foreach (string value in str.Split('+')) { if (value.Trim() == "Ctrl") { modifiers = modifiers + (int)HotkeyModifiers.Control; } else if (value.Trim() == "Alt") { modifiers = modifiers + (int)HotkeyModifiers.Alt; } else if (value.Trim() == "Shift") { modifiers = modifiers + (int)HotkeyModifiers.Shift; } else { if (Regex.IsMatch(value, @"[0-9]")) { vk = (Keys)Enum.Parse(typeof(Keys), "D" + value.Trim()); } else { vk = (Keys)Enum.Parse(typeof(Keys), value.Trim()); } } } //这里注册了Ctrl+Alt+E 快捷键 return(RegistGlobalHotKey(Handle, modifiers, vk, callback)); }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="window">持有快捷键窗口</param> /// <param name="fsModifiers">组合键</param> /// <param name="key">快捷键</param> /// <param name="callBack">回调函数</param> public static bool Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack) { var hwnd = new WindowInteropHelper(window).Handle; var _hwndSource = HwndSource.FromHwnd(hwnd); if (keyid == 10) { _hwndSource.AddHook(WndProc); } int id = keyid++; var vk = KeyInterop.VirtualKeyFromKey(key); if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) { //throw new Exception("regist hotkey fail."); return(false); } keymap[id] = callBack; return(true); }
//注册快捷键 public void Regist(IntPtr hWnd, string controlAndKeys, HotKeyCallBackHanlder callBack) { if (controlAndKeys == "") { return; } int modifiers = 0; Keys vk = Keys.None; foreach (string value in controlAndKeys.Split('+')) { if (value.Trim().ToUpper() == "CTRL") { modifiers = modifiers + (int)GlobalHotKey.HotkeyModifiers.Control; } else if (value.Trim().ToUpper() == "ALT") { modifiers = modifiers + (int)GlobalHotKey.HotkeyModifiers.Alt; } else if (value.Trim().ToUpper() == "SHIFT") { modifiers = modifiers + (int)GlobalHotKey.HotkeyModifiers.Shift; } else { if (Regex.IsMatch(value, @"[0-9]")) { vk = (Keys)Enum.Parse(typeof(Keys), "D" + value.Trim()); } else { vk = (Keys)Enum.Parse(typeof(Keys), value.Trim()); } } } Regist(hWnd, modifiers, vk, callBack); }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="hWnd"></param> /// <param name="modifiers"></param> /// <param name="key"></param> /// <param name="callBack"></param> public void Register(IntPtr hWnd, int modifiers, Keys key, HotKeyCallBackHanlder callBack = null) { var registerRecord = _hotkeyRegisterRecords.FirstOrDefault(i => i.IntPtr == hWnd && i.Modifiers == modifiers && i.Key == key); if (registerRecord != null) { UnregisterHotKey(hWnd, registerRecord.Id); _hotkeyRegisterRecords.Remove(registerRecord); } int id = registerId++; if (!RegisterHotKey(hWnd, id, modifiers, key)) { throw new Exception("注册失败!"); } _hotkeyRegisterRecords.Add(new HotkeyRegisterRecord() { Id = id, IntPtr = hWnd, Modifiers = modifiers, Key = key, CallBack = callBack }); }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="hWnd">持有快捷键窗口的句柄</param> /// <param name="fsModifiers">组合键</param> /// <param name="vk">快捷键的虚拟键码</param> /// <param name="callBack">回调函数</param> public static void Regist(IntPtr hWnd, Gean.User32.GlobalHotkeyModifiers fsModifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!User32.RegisterGlobalHotkey(hWnd, id, fsModifiers, vk)) { throw new Exception("regist hotkey fail."); } keymap[id] = callBack; }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="hWnd">持有快捷键窗口的句柄</param> /// <param name="fsModifiers">组合键</param> /// <param name="vk">快捷键的虚拟键码</param> /// <param name="callBack">回调函数</param> public static void Regist(IntPtr hWnd, HotkeyModifiers fsModifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, fsModifiers, vk)) return; keymap[id] = callBack; }