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.3 kB

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