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); }
private bool RegHotkeyFromString(string hotkeyStr, string callbackName, Label indicator = 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); if (indicator != null) { indicator.ResetBackColor(); } return(true); } else { var hotkey = HotKeys.Str2HotKey(hotkeyStr); if (hotkey == null) { MessageBox.Show(string.Format(I18N.GetString("Cannot parse hotkey: {0}"), hotkeyStr)); return(false); } else { bool regResult = (HotKeys.RegHotkey(hotkey, callback)); if (indicator != null) { indicator.BackColor = regResult ? Color.Green : Color.Yellow; } return(regResult); } } }