private bool TryRegHotkey(TextBox tb) { var hotkey = HotKeys.Str2HotKey(tb.Text); if (hotkey == null) { MessageBox.Show(string.Format(I18N.GetString("Cannot parse hotkey: {0}"), tb.Text)); tb.Clear(); return(false); } HotKeys.HotKeyCallBackHandler callBack; Label lb; PrepareForHotkey(tb, out callBack, out lb); UnregPrevHotkey(callBack); // try to register keys // if already registered by other progs // notify to change // use the corresponding label color to indicate // reg result. // Green: not occupied by others and operation succeed // Yellow: already registered by other program and need action: disable by clear the content // or change to another one // Transparent without color: first run or empty config bool regResult = HotKeys.Regist(hotkey, callBack); lb.BackColor = regResult ? Color.Green : Color.Yellow; return(regResult); }
public void TestStr2HotKey() { Assert.IsTrue(HotKeys.Str2HotKey("Ctrl+A").Equals(new HotKey(Key.A, ModifierKeys.Control))); Assert.IsTrue( HotKeys.Str2HotKey("Ctrl+Alt+A").Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt)))); Assert.IsTrue( HotKeys.Str2HotKey("Ctrl+Shift+A") .Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Shift)))); Assert.IsTrue( HotKeys.Str2HotKey("Ctrl+Alt+Shift+A") .Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift)))); HotKey testKey0 = HotKeys.Str2HotKey("Ctrl+Alt+Shift+A"); Assert.IsTrue(testKey0 != null && testKey0.Equals(new HotKey(Key.A, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift)))); HotKey testKey1 = HotKeys.Str2HotKey("Ctrl+Alt+Shift+F2"); Assert.IsTrue(testKey1 != null && testKey1.Equals(new HotKey(Key.F2, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift)))); HotKey testKey2 = HotKeys.Str2HotKey("Ctrl+Shift+Alt+D7"); Assert.IsTrue(testKey2 != null && testKey2.Equals(new HotKey(Key.D7, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift)))); HotKey testKey3 = HotKeys.Str2HotKey("Ctrl+Shift+Alt+NumPad7"); Assert.IsTrue(testKey3 != null && testKey3.Equals(new HotKey(Key.NumPad7, (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift)))); }
public static void RegHotkey() { var _hotKeyConf = Configuration.Load().hotkey; if (_hotKeyConf == null || !_hotKeyConf.RegHotkeysAtStartup) { return; } var _hotKeyDic = new Dictionary <HotKey, HotKeys.HotKeyCallBackHandler>(); try { MethodInfo[] fis = typeof(HotkeyCallbacks).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); Type ht = _hotKeyConf.GetType(); for (int i = 0; i < fis.Length; i++) { if (fis[i].Name.EndsWith("Callback")) { var callbackName = fis[i].Name; var fieldName = callbackName.Replace("Callback", ""); var hk = HotKeys.Str2HotKey(ht.GetField(fieldName).GetValue(_hotKeyConf) as string); var cb = HotkeyCallbacks.GetCallback(callbackName) as HotKeys.HotKeyCallBackHandler; if (hk != null && cb != null) { _hotKeyDic.Add(hk, cb); } } } int regCount = 0; foreach (var v in _hotKeyDic) { if (!HotKeys.RegHotkey(v.Key, v.Value)) { foreach (var k in _hotKeyDic) { if (regCount > 0) { HotKeys.UnregExistingHotkey(k.Value); regCount--; } } MessageBox.Show(I18N.GetString("Register hotkey failed"), I18N.GetString("Shadowsocks")); return; } regCount++; } } catch (Exception e) { Logging.Error(e); } }
public static bool RegHotkeyFromString(string hotkeyStr, string callbackName, Action <RegResult> onComplete = null) { var _callback = HotkeyCallbacks.GetCallback(callbackName); if (_callback == null) { throw new Exception($"{callbackName} not found"); } var callback = _callback as HotKeys.HotKeyCallBackHandler; if (hotkeyStr.IsNullOrEmpty()) { HotKeys.UnregExistingHotkey(callback); onComplete?.Invoke(RegResult.UnregSuccess); return(true); } else { var hotkey = HotKeys.Str2HotKey(hotkeyStr); if (hotkey == null) { Logging.Error($"Cannot parse hotkey: {hotkeyStr}"); onComplete?.Invoke(RegResult.ParseError); return(false); } else { bool regResult = (HotKeys.RegHotkey(hotkey, callback)); if (regResult) { onComplete?.Invoke(RegResult.RegSuccess); } else { onComplete?.Invoke(RegResult.RegFailure); } return(regResult); } } }