Esempio n. 1
0
        public Config()
        {
            var file = ConfigFile;

            if (file.Exists)
            {
                var  saved      = File.ReadAllLines(file.FullName);
                Type configType = typeof(Config);
                foreach (string line in saved)
                {
                    var splitted = line.Split('=');
                    var prop     = configType.GetProperty(splitted[0]);
                    if (prop != null)
                    {
                        object value = splitted[1];
                        if ((string)value == "null" || string.IsNullOrEmpty((string)value))
                        {
                            value = null;
                        }
                        else if (prop.PropertyType.IsEnum)
                        {
                            value = Enum.Parse(prop.PropertyType, (string)value);
                        }
                        else if (prop.PropertyType == typeof(KeyboardShortcut))
                        {
                            value = new KeyboardShortcut((string)value);
                        }
                        prop.SetValue(this, value);
                    }
                }
            }
            this.Apply();
        }
Esempio n. 2
0
        public static KeyboardShortcutAction CallActionIfRegistered(KeyboardShortcut key)
        {
            var action = RegisteredActions.Values.FirstOrDefault((hotKey) => hotKey.Shortcut.ToString() == key.ToString());

            if (action != null)
            {
                action.Action.Invoke();
                return(action);
            }
            return(null);
        }
Esempio n. 3
0
        private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, ref KBHookStruct lParam)
        {
            if (nCode == 0)
            {
                var wpfKey      = KeyInterop.KeyFromVirtualKey(lParam.vkCode);
                var wparamTyped = wParam.ToInt32();
                if (Enum.IsDefined(typeof(KeyboardState), wparamTyped))
                {
                    var isKeyDown = false;
                    if (wparamTyped == (int)KeyboardState.WM_KEYDOWN || wparamTyped == (int)KeyboardState.WM_SYSKEYDOWN)
                    {
                        isKeyDown = true;
                        if (!CurrentlyPressedKeys.Contains(wpfKey))
                        {
                            CurrentlyPressedKeys.Add(wpfKey);
                        }
                    }
                    else if (CurrentlyPressedKeys.Contains(wpfKey))
                    {
                        CurrentlyPressedKeys.Remove(wpfKey);
                    }
                    else
                    {
                        var aaa = wparamTyped;
                    }

                    var key = new KeyboardShortcut(wpfKey)
                    {
                        CtrlModifier  = CurrentlyPressedKeys.Contains(Key.LeftCtrl) || CurrentlyPressedKeys.Contains(Key.LeftCtrl),
                        ShiftModifier = CurrentlyPressedKeys.Contains(Key.LeftShift) || CurrentlyPressedKeys.Contains(Key.RightShift),
                        AltModifier   = CurrentlyPressedKeys.Contains(Key.LeftAlt) || CurrentlyPressedKeys.Contains(Key.RightAlt),
                        WinModifier   = CurrentlyPressedKeys.Contains(Key.LWin) || CurrentlyPressedKeys.Contains(Key.RWin)
                    };
                    var keyEvent = new KeyEvent(key);

                    if (HandlerControls != null)
                    {
                        foreach (var control in HandlerControls)
                        {
                            if (control.CanHandleHook)
                            {
                                control.HandleHook(keyEvent);
                                if (keyEvent.Handled)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    if (keyEvent.Handled)
                    {
                        return(new IntPtr(1));
                    }
                    else if (!keyEvent.Handled && isKeyDown)
                    {
                        KeyboardShortcutAction action = HotKeyManager.CallActionIfRegistered(key);
                        if (action != null)
                        {
                            return(new IntPtr(1));
                        }
                    }
                }
            }

            return(CallNextHookEx(CurrentHook, nCode, wParam, ref lParam));
        }
Esempio n. 4
0
 public KeyEvent(KeyboardShortcut key)
 {
     this.Key = key;
 }
 public KeyboardShortcutAction(int id, KeyboardShortcut shortcut, Action action)
 {
     this.Id       = id;
     this.Shortcut = shortcut;
     this.Action   = action;
 }