public void AddHotKey(HotKey hotKey, string actionName, bool save = true)
		{
			RemoveHotKey(hotKey, false);
			HotKeys.Add(new HotKeyConfigItem(hotKey, actionName));
			if(save)
				Save();
		}
		public static bool RegisterHotkey(HotKey hotKey, Action action, string name)
		{
			if(RegisteredHotKeys.ContainsKey(hotKey))
			{
				Log.Warn($"[{hotKey}] already registered.");
				return false;
			}
			try
			{
				var id = KeyboardHook.RegisterHotKey(hotKey.Mod, hotKey.Key);
				HotKeyIds.Add(hotKey, id);
				RegisteredHotKeys.Add(hotKey, action);

				var predefined = PredefinedHotKeyActions.PredefinedActionNames.FirstOrDefault(x => x.MethodName == name);
				var title = predefined != null ? predefined.Title : name;
				Log.Info($"Registering [{hotKey}]: {title}.");
				RegisteredHotKeysInfo.Add(new KeyValuePair<HotKey, string>(hotKey, title));
				return true;
			}
			catch(Exception ex)
			{
				Log.Error(ex);
				return false;
			}
		}
		public void RemoveHotKey(HotKey hotKey, bool save = true)
		{
			var existing = HotKeys.FirstOrDefault(x => x.HotKey.Equals(hotKey));
			if(existing != null)
				HotKeys.Remove(existing);
			if(save)
				Save();
		}
		public static bool AddPredefinedHotkey(HotKey hotKey, string actionName)
		{
			if(LoadPredefinedHotkeyAction(hotKey, actionName))
			{
				HotKeyConfig.Instance.AddHotKey(hotKey, actionName);
				return true;
			}
			return false;
		}
		public static void RemovePredefinedHotkey(HotKey hotKey)
		{
			HotKeyConfig.Instance.RemoveHotKey(hotKey);
			if(_registeredHotKeys.ContainsKey(hotKey))
				_registeredHotKeys.Remove(hotKey);
			var info = RegisteredHotKeysInfo.FirstOrDefault(x => x.Key.Equals(hotKey));
			if(!info.Equals(default(KeyValuePair<HotKey, string>)))
				RegisteredHotKeysInfo.Remove(info);
			try
			{
				KeyboardHook.UnRegisterHotKey(_hotKeyIds[hotKey]);
				_hotKeyIds.Remove(hotKey);
			}
			catch(Exception ex)
			{
				Logger.WriteLine("Error removing hotkey: " + ex, "HotKeyManager");
			}
		}
		public static void RemovePredefinedHotkey(HotKey hotKey)
		{
			Log.Info($"Removing [{hotKey}].");
			HotKeyConfig.Instance.RemoveHotKey(hotKey);
			if(RegisteredHotKeys.ContainsKey(hotKey))
				RegisteredHotKeys.Remove(hotKey);
			var info = RegisteredHotKeysInfo.FirstOrDefault(x => x.Key.Equals(hotKey));
			if(!info.Equals(default(KeyValuePair<HotKey, string>)))
				RegisteredHotKeysInfo.Remove(info);
			try
			{
				KeyboardHook.UnRegisterHotKey(HotKeyIds[hotKey]);
				HotKeyIds.Remove(hotKey);
			}
			catch(Exception ex)
			{
				Log.Error(ex);
			}
		}
		public static bool RegisterHotkey(HotKey hotKey, Action action, string name)
		{
			if(_registeredHotKeys.ContainsKey(hotKey))
			{
				Logger.WriteLine(string.Format("HotKey {0} already registered.", hotKey), "HotKeyManager");
				return false;
			}
			try
			{
				var id = KeyboardHook.RegisterHotKey(hotKey.Mod, hotKey.Key);
				_hotKeyIds.Add(hotKey, id);
				_registeredHotKeys.Add(hotKey, action);

				var predefined = PredefinedHotKeyActions.PredefinedActionNames.FirstOrDefault(x => x.MethodName == name);
				var title = predefined != null ? predefined.Title : name;
				RegisteredHotKeysInfo.Add(new KeyValuePair<HotKey, string>(hotKey, title));
				return true;
			}
			catch(Exception ex)
			{
				Logger.WriteLine(ex.ToString(), "HotKeyManager");
				return false;
			}
		}
		public bool Equals(HotKey hotKey) => hotKey.Mod == Mod && hotKey.Key == Key;
		private static bool LoadPredefinedHotkeyAction(HotKey hotKey, string actionName)
		{
			var action = typeof(PredefinedHotKeyActions).GetMethods().FirstOrDefault(x => x.Name == actionName);
			if(action != null)
				return RegisterHotkey(hotKey, () => action.Invoke(null, null), actionName);
			Logger.WriteLine(string.Format("Could not find predefined action \"{0}\"", actionName), "HotKeyManager");
			HotKeyConfig.Instance.RemoveHotKey(hotKey);
			return false;
		}
			public HotKeyConfigItem(HotKey hotKey, string action)
			{
				Key = hotKey.Key;
				Mod = hotKey.Mod;
				Action = action;
			}
		public bool Equals(HotKey hotKey)
		{
			return hotKey.Mod == Mod && hotKey.Key == Key;
		}
Example #12
0
 public HotKeyConfigItem(HotKey hotKey, string action)
 {
     Key    = hotKey.Key;
     Mod    = hotKey.Mod;
     Action = action;
 }
 public bool Equals(HotKey hotKey) => hotKey.Mod == Mod && hotKey.Key == Key;