You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

HotkeyReg.cs 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using NLog;
  5. using Shadowsocks.Controller.Hotkeys;
  6. using Shadowsocks.Model;
  7. namespace Shadowsocks.Controller
  8. {
  9. static class HotkeyReg
  10. {
  11. private static Logger logger = LogManager.GetCurrentClassLogger();
  12. public static void RegAllHotkeys()
  13. {
  14. var hotkeyConfig = Configuration.Load().hotkey;
  15. if (hotkeyConfig == null || !hotkeyConfig.RegHotkeysAtStartup)
  16. return;
  17. // if any of the hotkey reg fail, undo everything
  18. if (RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback")
  19. && RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback")
  20. && RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback")
  21. && RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback")
  22. && RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback")
  23. && RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback")
  24. )
  25. {
  26. // success
  27. }
  28. else
  29. {
  30. RegHotkeyFromString("", "SwitchSystemProxyCallback");
  31. RegHotkeyFromString("", "SwitchSystemProxyModeCallback");
  32. RegHotkeyFromString("", "SwitchAllowLanCallback");
  33. RegHotkeyFromString("", "ShowLogsCallback");
  34. RegHotkeyFromString("", "ServerMoveUpCallback");
  35. RegHotkeyFromString("", "ServerMoveDownCallback");
  36. MessageBox.Show(I18N.GetString("Register hotkey failed"), I18N.GetString("Shadowsocks"));
  37. }
  38. }
  39. public static bool RegHotkeyFromString(string hotkeyStr, string callbackName, Action<RegResult> onComplete = null)
  40. {
  41. var _callback = HotkeyCallbacks.GetCallback(callbackName);
  42. if (_callback == null)
  43. {
  44. throw new Exception($"{callbackName} not found");
  45. }
  46. var callback = _callback as HotKeys.HotKeyCallBackHandler;
  47. if (hotkeyStr.IsNullOrEmpty())
  48. {
  49. HotKeys.UnregExistingHotkey(callback);
  50. onComplete?.Invoke(RegResult.UnregSuccess);
  51. return true;
  52. }
  53. else
  54. {
  55. var hotkey = HotKeys.Str2HotKey(hotkeyStr);
  56. if (hotkey == null)
  57. {
  58. logger.Error($"Cannot parse hotkey: {hotkeyStr}");
  59. onComplete?.Invoke(RegResult.ParseError);
  60. return false;
  61. }
  62. else
  63. {
  64. bool regResult = (HotKeys.RegHotkey(hotkey, callback));
  65. if (regResult)
  66. {
  67. onComplete?.Invoke(RegResult.RegSuccess);
  68. }
  69. else
  70. {
  71. onComplete?.Invoke(RegResult.RegFailure);
  72. }
  73. return regResult;
  74. }
  75. }
  76. }
  77. public enum RegResult
  78. {
  79. RegSuccess,
  80. RegFailure,
  81. ParseError,
  82. UnregSuccess,
  83. //UnregFailure
  84. }
  85. }
  86. }