public void Has_same_hash_code_when_order_is_reversed() { var keyArg1 = new KeyCombination(Key.Left, Key.Right); var keyArg2 = new KeyCombination(Key.Right, Key.Left); keyArg1.GetHashCode().Should().Be(keyArg2.GetHashCode()); }
/// <summary> /// Register a keyboard hook event /// </summary> /// <param name="keys">The short keys. minimum is two keys</param> /// <param name="execute">The action to run when the key ocmbination has pressed</param> /// <param name="message">Empty if no error occurred otherwise error message</param> /// <param name="runAsync">True if the action should execute in the background. -Be careful from thread affinity- Default is false</param> /// <param name="dispose">An action to run when unsubscribing from keyboard hook. can be null</param> /// <returns>Event id to use when unregister</returns> public int Hook(List <Key> keys, Action execute, out string message, bool runAsync = false, Action <object> dispose = null) { if (_hookEvents == null) { message = "Can't register"; return(-1); } if (keys == null || execute == null) { message = "'keys' and 'execute' can't be null"; return(-1); } if (keys.Count < 2) { message = "You must provide at least two keys"; return(-1); } if (!ValidateKeys(keys)) { message = "Unallowed key. Only 'shift', 'ctrl' and 'a' - 'z' are allowed"; //return -1; } var kc = new KeyCombination(keys); int id = kc.GetHashCode(); if (_hookEvents.ContainsKey(id)) { message = "The key combination is already exist it the application"; return(-1); } // if the action should run async, wrap it with Task Action asyncAction = null; if (runAsync) { asyncAction = () => Task.Run(() => execute); } _hookEvents[id] = new KeyValuePair <KeyCombination, HookActions>(kc, new HookActions(asyncAction ?? execute, dispose)); message = string.Empty; return(id); }