public void CancelHotkeyBind(string bindId) { if (_bindState != null) { lock (GetLock()) { if (_bindState != null) { Logger.Info("[" + _bindState.bindId + "] Hotkey Bind - CANCEL"); _bindState = null; } } } }
/// <summary> /// Attempts to unbind and return the hotkey specified by the bindId. Returns null if we aren't in the bind state or if the hotkey does not exist. /// </summary> /// <param name="bindId"></param> /// <returns></returns> public Hotkey UnbindHotkey(string bindId) { if (_bindState != null) { lock (GetLock()) { if (_bindState != null) { Hotkey hotkey = ServiceWrapper.config.SetHotkeyKey(_bindState.hotkeyId, null); Logger.Info("[" + _bindState.bindId + "] Hotkey Bind - UNBIND" + (hotkey != null ? "" : " FAILED: Hotkey " + _bindState.hotkeyId + " did not exist")); _bindState = null; return(hotkey); } } } return(null); }
/// <summary> /// Begins binding the specified hotkey and returns the bind ID which is a unique number assigned to this bind session. /// If the hotkey ID is invalid, returns null. /// If a bind operation is already in progress, returns empty string. /// </summary> /// <param name="id">The unique identifier of the hotkey to be bound.</param> /// <returns></returns> public string BeginHotkeyBind(int id) { lock (GetLock()) { Hotkey hotkey = ServiceWrapper.config.hotkeys.Get(id); if (hotkey == null) { return(null); } if (GetCurrentBindState() != null) { return(""); } _bindState = new BindState(id); Logger.Info("[" + _bindState.bindId + "] Hotkey Bind - START \"" + hotkey.name + "\""); return(GetCurrentBindId()); } }
/// <summary> /// Call when any key is pressed. Returns true if the key was consumed by a keybind event. If false, the key press may be used to trigger hotkeys. /// </summary> /// <param name="key">The key code.</param> /// <returns></returns> private bool HandlePossibleKeybind(int key) { if (GetCurrentBindState() == null) { return(false); } lock (GetLock()) { BindState state = GetCurrentBindState(); if (state == null) { return(false); } Logger.Info("[" + _bindState.bindId + "] Hotkey Bind - SET KEY " + ConsoleKeyHelper.GetKeyName(key)); _bindState = null; return(ServiceWrapper.config.SetHotkeyKey(state.hotkeyId, key) != null); } }
/// <summary> /// Returns the current bind state object which will be null if no bind operation is active. /// </summary> /// <returns></returns> private BindState GetCurrentBindState() { BindState bs = _bindState; long now = TimeUtil.GetTimeInMsSinceEpoch(); if (bs != null && bs.bindStart + 15000 < now) { lock (GetLock()) { // We found the existing state was expired, so we entered the lock. Now check again. if (_bindState != null && _bindState.bindStart + 15000 < now) { Logger.Info("[" + _bindState.bindId + "] Hotkey Bind - TIMEOUT"); _bindState = null; } return(_bindState); } } else { return(bs); // Not expired, but may be null } }