/// <summary>
        /// Find correct callback and corresponding label
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="cb"></param>
        /// <param name="lb"></param>
        private void PrepareForHotkey(TextBox tb, out HotKeys.HotKeyCallBackHandler cb, out Label lb)
        {
            /*
             * XXX: The labelName, TextBoxName and callbackName
             *      must follow this rule to make use of reflection
             *
             *      <BaseName><Control-Type-Name>
             */
            if (tb == null)
            {
                throw new ArgumentNullException(nameof(tb));
            }

            var pos          = tb.Name.LastIndexOf("TextBox", StringComparison.OrdinalIgnoreCase);
            var rawName      = tb.Name.Substring(0, pos);
            var labelName    = rawName + "Label";
            var callbackName = rawName + "Callback";

            var callback = HotkeyCallbacks.GetCallback(callbackName);

            if (callback == null)
            {
                throw new Exception($"{callbackName} not found");
            }
            cb = callback as HotKeys.HotKeyCallBackHandler;

            var label = GetFieldViaName(GetType(), labelName, this);

            if (label == null)
            {
                throw new Exception($"{labelName} not found");
            }
            lb = label as Label;
        }
        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);
            }
        }
Esempio n. 3
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);
                }
            }
        }
        private bool RegHotkeyFromString(string hotkeyStr, string callbackName, Label indicator = 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);
                if (indicator != null)
                {
                    indicator.ResetBackColor();
                }
                return(true);
            }
            else
            {
                var hotkey = HotKeys.Str2HotKey(hotkeyStr);
                if (hotkey == null)
                {
                    MessageBox.Show(string.Format(I18N.GetString("Cannot parse hotkey: {0}"), hotkeyStr));
                    return(false);
                }
                else
                {
                    bool regResult = (HotKeys.RegHotkey(hotkey, callback));
                    if (indicator != null)
                    {
                        indicator.BackColor = regResult ? Color.Green : Color.Yellow;
                    }
                    return(regResult);
                }
            }
        }