public static void RegHotkey()
        {
            var _hotKeyConf = Configuration.Load().hotkey;

            if (_hotKeyConf == null || !_hotKeyConf.RegHotkeysAtStartup)
            {
                return;
            }

            var _hotKeyDic = new Dictionary <HotKey, HotKeys.HotKeyCallBackHandler>();

            try
            {
                MethodInfo[] fis = typeof(HotkeyCallbacks).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                Type         ht  = _hotKeyConf.GetType();
                for (int i = 0; i < fis.Length; i++)
                {
                    if (fis[i].Name.EndsWith("Callback"))
                    {
                        var callbackName = fis[i].Name;
                        var fieldName    = callbackName.Replace("Callback", "");

                        var hk = HotKeys.Str2HotKey(ht.GetField(fieldName).GetValue(_hotKeyConf) as string);
                        var cb = HotkeyCallbacks.GetCallback(callbackName) as HotKeys.HotKeyCallBackHandler;

                        if (hk != null && cb != null)
                        {
                            _hotKeyDic.Add(hk, cb);
                        }
                    }
                }

                int regCount = 0;
                foreach (var v in _hotKeyDic)
                {
                    if (!HotKeys.RegHotkey(v.Key, v.Value))
                    {
                        foreach (var k in _hotKeyDic)
                        {
                            if (regCount > 0)
                            {
                                HotKeys.UnregExistingHotkey(k.Value);
                                regCount--;
                            }
                        }
                        MessageBox.Show(I18N.GetString("Register hotkey failed"), I18N.GetString("Shadowsocks"));
                        return;
                    }
                    regCount++;
                }
            }
            catch (Exception e)
            {
                Logging.Error(e);
            }
        }
Example #2
0
        public static bool RegHotkeyFromString(string hotkeyStr, string callbackName, Action <RegResult> onComplete = null)
        {
            var _callback = HotkeyCallbacks.GetCallback(callbackName);

            if (_callback == null)
            {
                throw new Exception($"{callbackName} not found");
            }

            var callback = _callback as HotKeys.HotKeyCallBackHandler;

            if (hotkeyStr.IsNullOrEmpty())
            {
                HotKeys.UnregExistingHotkey(callback);
                onComplete?.Invoke(RegResult.UnregSuccess);
                return(true);
            }
            else
            {
                var hotkey = HotKeys.Str2HotKey(hotkeyStr);
                if (hotkey == null)
                {
                    Logging.Error($"Cannot parse hotkey: {hotkeyStr}");
                    onComplete?.Invoke(RegResult.ParseError);
                    return(false);
                }
                else
                {
                    bool regResult = (HotKeys.RegHotkey(hotkey, callback));
                    if (regResult)
                    {
                        onComplete?.Invoke(RegResult.RegSuccess);
                    }
                    else
                    {
                        onComplete?.Invoke(RegResult.RegFailure);
                    }
                    return(regResult);
                }
            }
        }