Example #1
0
        /// <summary>
        /// Translates a virtual key to its character equivalent using a specified keyboard layout
        /// </summary>
        /// <param name="virtualKeyCode"></param>
        /// <param name="scanCode"></param>
        /// <param name="fuState"></param>
        /// <param name="dwhkl"></param>
        /// <param name="ch"></param>
        /// <returns></returns>
        internal static bool TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, uint dwhkl, out char ch)
        {
            StringBuilder pwszBuff      = new StringBuilder(64);
            KeyboardState keyboardState = KeyboardState.GetCurrent();

            byte[] currentKeyboardState = keyboardState.GetNativeState();

            if (ToUnicodeEx(virtualKeyCode, scanCode, currentKeyboardState, pwszBuff, pwszBuff.Capacity, fuState, dwhkl) != 1)
            {
                ch = ( char )0;
                return(false);
            }

            ch = pwszBuff[0];

            bool isDownShift       = keyboardState.IsDown(Keys.ShiftKey);
            bool isToggledCapsLock = keyboardState.IsToggled(Keys.CapsLock);

            if ((isToggledCapsLock ^ isDownShift) && Char.IsLetter(ch))
            {
                ch = Char.ToUpper(ch);
            }

            return(true);
        }
        /// <summary>
        ///     Detects a key or key combination and triggers the corresponding action.
        /// </summary>
        /// <param name="source">
        ///     An instance of Global or Application hook. Use <see cref="Hook.GlobalEvents" /> or <see cref="Hook.AppEvents" /> to
        ///     create it.
        /// </param>
        /// <param name="map">
        ///     This map contains the list of key combinations mapped to corresponding actions. You can use a dictionary initilizer
        ///     to easily create it.
        ///     Whenever a listed combination will be detected a corresponding action will be triggered.
        /// </param>
        /// <param name="reset">
        ///     This optional action will be executed when some key was pressed but it was not part of any wanted combinations.
        /// </param>
        public static void OnCombination(this IKeyboardEvents source,
                                         IEnumerable <KeyValuePair <Combination, Action> > map, Action reset = null)
        {
            var watchlists = map.GroupBy(k => k.Key.TriggerKey)
                             .ToDictionary(g => g.Key, g => g.ToArray());

            source.KeyDown += (sender, e) =>
            {
                var found = watchlists.TryGetValue(e.KeyCode, out KeyValuePair <Combination, Action>[] element);
                if (!found)
                {
                    reset?.Invoke();
                    return;
                }
                var state     = KeyboardState.GetCurrent();
                var action    = reset;
                var maxLength = 0;
                foreach (var current in element)
                {
                    var matches = current.Key.Chord.All(state.IsDown);
                    if (!matches)
                    {
                        continue;
                    }
                    if (maxLength > current.Key.ChordLength)
                    {
                        continue;
                    }
                    maxLength = current.Key.ChordLength;
                    action    = current.Value;
                }
                action?.Invoke();
            };
        }
Example #3
0
        /// <summary>
        /// Detects a key or key combination and triggers the corresponding action.
        /// </summary>
        /// <param name="source">
        /// An instance of Global or Application hook. Use <see cref="Hook.GlobalEvents" /> or <see cref="Hook.AppEvents" /> to
        /// create it.
        /// </param>
        /// <param name="map">
        /// This map contains the list of key combinations mapped to corresponding actions. You can use a dictionary initilizer
        /// to easily create it.
        /// Whenever a listed combination will be detected a corresponding action will be triggered.
        /// </param>
        /// <param name="reset">
        /// This optional action will be executed when some key was pressed but it was not part of any wanted combinations.
        /// </param>
        public static void OnCXCombination(this IKeyboardEvents source, string key, Dictionary <string, Action> map, Action reset = null)
        {
            source.KeyDown += (sender, e) =>
            {
                Action action = reset;
                if (e.KeyCode.ToString() == key)
                {
                    var state = KeyboardState.GetCurrent();


                    var hotkeyString = string.Empty;
                    if (state.IsDown(Keys.Control))
                    {
                        hotkeyString += "Control+";
                    }
                    if (state.IsDown(Keys.Shift))
                    {
                        hotkeyString += "Shift+";
                    }
                    if (state.IsDown(Keys.Alt))
                    {
                        hotkeyString += "Alt+";
                    }
                    hotkeyString += e.KeyCode.ToString();

                    map.TryGetValue(hotkeyString, out action);
                }
                action?.Invoke();
            };
        }
Example #4
0
        private void HooksOnKeyUp(object _sender, KeyEventArgs _e)
        {
            if (m_watchList == null || !m_isNeedTracking)
            {
                return;
            }

            if (m_watchList.TryGetValue(_e.KeyCode, out var element))
            {
                var    state     = KeyboardState.GetCurrent();
                Action action    = null;
                var    maxLength = 0;

                foreach (var current in element)
                {
                    var matches = current.Key.Chord.All(state.IsDown);

                    if (!matches)
                    {
                        continue;
                    }

                    if (maxLength > current.Key.ChordLength)
                    {
                        continue;
                    }

                    maxLength = current.Key.ChordLength;
                    action    = current.Value;
                }

                action?.Invoke();
            }
        }
        public void ReleaseDownKeys()
        {
            var downKeys = KeyboardState.GetCurrent().AllDownKeys;

            foreach (var downKey in downKeys)
            {
                Up((Key)downKey);
            }
        }
Example #6
0
 // Token: 0x06000225 RID: 549 RVA: 0x0000DC78 File Offset: 0x0000BE78
 private void InitializeKeys()
 {
     foreach (Keys key in this.HotKeys)
     {
         if (this.m_hotkeystate.ContainsKey(key))
         {
             this.m_hotkeystate.Add(key, false);
         }
         this.m_hotkeystate[key] = KeyboardState.GetCurrent().IsDown(key);
     }
 }
Example #7
0
        private void InitializeKeys()
        {
            foreach (Keys k in HotKeys)
            {
                if (m_hotkeystate.ContainsKey(k))
                {
                    m_hotkeystate.Add(k, false);
                }

                m_hotkeystate[k] = KeyboardState.GetCurrent().IsDown(k);
            }
        }
Example #8
0
        /// <summary>
        ///     Adds the keys into the dictionary tracking the keys and gets the real-time status of the Keys
        ///     from the OS
        /// </summary>
        private void InitializeKeys()
        {
            foreach (var k in HotKeys)
            {
                if (m_hotkeystate.ContainsKey(k))
                {
                    m_hotkeystate.Add(k, false);
                }

                //assign using the current state of the keyboard
                m_hotkeystate[k] = KeyboardState.GetCurrent().IsDown(k);
            }
        }
        private void AssociatedObjectOnKeyDown(object _sender, KeyEventArgs _e)
        {
            var state = KeyboardState.GetCurrent();

            var pressedModifiers = m_modifierKeys.Where(_key => state.IsDown((System.Windows.Forms.Keys)_key))
                                   .Select(_x => _x.ToString());
            var pressedOthers = m_keys.Where(_key => state.IsDown((System.Windows.Forms.Keys)_key))
                                .Select(_x => _x.ToString());

            var allPressed = pressedModifiers.Union(pressedOthers);

            _e.Handled = true;

            AssociatedObject.Text = string.Join("+", allPressed);
        }
Example #10
0
        public static int Main(string[] args)
        {
            try
            {
                var shiftDown = KeyboardState.GetCurrent().IsDown(Key.Shift);
                if (shiftDown && !Context.IsElevated)
                {
                    // so you could pin metatool to the first windows taskbar shortcut,
                    // and use Win+Shift+1 then Alt+Y to launch as admin,
                    // note: could not use Win+Alt+1, it's used to do right click on the first taskbar item
                    return(Context.Restart(0, true));
                }
                else
                {
                    ServiceConfig.BuildHost(args).Run();
                }
                return(0);
            }
            catch (Exception e)
            {
                if (e is AggregateException aggregateEx)
                {
                    e = aggregateEx.Flatten().InnerException;
                }

                if (e is CompilationErrorException)
                {
                    // no need to write out anything as the upstream services will report that
                    return(0x1);
                }

                // Be verbose (stack trace) in debug mode otherwise brief
                var error = args.Any(arg => arg == DebugFlagShort ||
                                     arg == DebugFlagLong)
                    ? e.ToString()
                    : e.GetBaseException().Message;
                Services.CommonLogger.LogError($"Error: {error}");
                return(0x1);
            }
        }
Example #11
0
        /// <summary>
        /// Detects a key or key combination and triggers the corresponding action.
        /// </summary>
        /// <param name="source">
        /// An instance of Global or Application hook. Use <see cref="Hook.GlobalEvents" /> or <see cref="Hook.AppEvents" /> to
        /// create it.
        /// </param>
        /// <param name="map">
        /// This map contains the list of key combinations mapped to corresponding actions. You can use a dictionary initilizer
        /// to easily create it.
        /// Whenever a listed combination will be detected a corresponding action will be triggered.
        /// </param>
        /// <param name="reset">
        /// This optional action will be executed when some key was pressed but it was not part of any wanted combinations.
        /// </param>
        public static void OnCXCombination(this IKeyboardEvents source,
                                           IEnumerable <KeyValuePair <CXHotkeyCombination, Action> > map, Action reset = null)
        {
            var watchlists = map.GroupBy(k => k.Key.TriggerKey)
                             .ToDictionary(g => g.Key, g => g.ToArray());

            source.KeyDown += (sender, e) =>
            {
                if (!watchlists.TryGetValue(e.KeyCode, out KeyValuePair <CXHotkeyCombination, Action>[] element))
                {
                    reset?.Invoke();
                    return;
                }
                var state            = KeyboardState.GetCurrent();
                var action           = reset;
                var maxLength        = 0;
                int modifiersPressed = CountTrue(state.IsDown(Keys.Control), state.IsDown(Keys.Alt), state.IsDown(Keys.Shift));

                foreach (var current in element)
                {
                    if (current.Key.ChordLength < modifiersPressed)
                    {
                        continue;
                    }
                    if (!current.Key.Chord.All(state.IsDown))
                    {
                        continue;
                    }
                    if (maxLength > current.Key.ChordLength)
                    {
                        continue;
                    }
                    maxLength = current.Key.ChordLength;
                    action    = current.Value;
                }
                action?.Invoke();
            };
        }
Example #12
0
        internal static bool TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, out char ch)
        {
            KeyboardState keyboardState = KeyboardState.GetCurrent();

            byte[] nativekeyboardState = keyboardState.GetNativeState();

            if (ToUnicode(virtualKeyCode, scanCode, nativekeyboardState, m_pwszBuffer, m_pwszBuffer.Capacity, fuState) != 1)
            {
                ch = ( char )0;
                return(false);
            }

            ch = m_pwszBuffer[0];

            bool isDownShift       = keyboardState.IsDown(Keys.ShiftKey);
            bool isToggledCapsLock = keyboardState.IsToggled(Keys.CapsLock);

            if ((isToggledCapsLock ^ isDownShift) && Char.IsLetter(ch))
            {
                ch = Char.ToUpper(ch);
            }
            return(true);
        }
        /// <summary>
        ///     Translates a virtual key to its character equivalent using a specified keyboard layout
        /// </summary>
        /// <param name="virtualKeyCode"></param>
        /// <param name="scanCode"></param>
        /// <param name="fuState"></param>
        /// <param name="dwhkl"></param>
        /// <param name="chars"></param>
        /// <returns></returns>
        internal static void TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, IntPtr dwhkl,
                                                         out char[] chars)
        {
            var pwszBuff             = new StringBuilder(64);
            var keyboardState        = KeyboardState.GetCurrent();
            var currentKeyboardState = keyboardState.GetNativeState();
            var isDead = false;

            if (keyboardState.IsDown(Keys.ShiftKey))
            {
                currentKeyboardState[(byte)Keys.ShiftKey] = 0x80;
            }

            if (keyboardState.IsToggled(Keys.CapsLock))
            {
                currentKeyboardState[(byte)Keys.CapsLock] = 0x01;
            }

            var relevantChars = ToUnicodeEx(virtualKeyCode, scanCode, currentKeyboardState, pwszBuff, pwszBuff.Capacity,
                                            fuState, dwhkl);

            switch (relevantChars)
            {
            case -1:
                isDead = true;
                ClearKeyboardBuffer(virtualKeyCode, scanCode, dwhkl);
                chars = null;
                break;

            case 0:
                chars = null;
                break;

            case 1:
                if (pwszBuff.Length > 0)
                {
                    chars = new[] { pwszBuff[0] }
                }
                ;
                else
                {
                    chars = null;
                }
                break;

            // Two or more (only two of them is relevant)
            default:
                if (pwszBuff.Length > 1)
                {
                    chars = new[] { pwszBuff[0], pwszBuff[1] }
                }
                ;
                else
                {
                    chars = new[] { pwszBuff[0] }
                };
                break;
            }

            if (lastVirtualKeyCode != 0 && lastIsDead)
            {
                if (chars != null)
                {
                    var sbTemp = new StringBuilder(5);
                    ToUnicodeEx(lastVirtualKeyCode, lastScanCode, lastKeyState, sbTemp, sbTemp.Capacity, 0, dwhkl);
                    lastIsDead         = false;
                    lastVirtualKeyCode = 0;
                }

                return;
            }

            lastScanCode       = scanCode;
            lastVirtualKeyCode = virtualKeyCode;
            lastIsDead         = isDead;
            lastKeyState       = (byte[])currentKeyboardState.Clone();
        }
Example #14
0
 public static IObservable <KeyWithState> WithState(this IObservable <Keys> source)
 {
     return(source
            .Select(evt => new KeyWithState(evt, KeyboardState.GetCurrent())));
 }
        // Token: 0x06000330 RID: 816 RVA: 0x0000F534 File Offset: 0x0000D734
        internal static void TryGetCharFromKeyboardState(int virtualKeyCode, int scanCode, int fuState, IntPtr dwhkl, out char[] chars)
        {
            StringBuilder stringBuilder = new StringBuilder(64);
            KeyboardState current       = KeyboardState.GetCurrent();

            byte[] nativeState = current.GetNativeState();
            bool   flag        = false;

            if (current.IsDown(Keys.ShiftKey))
            {
                nativeState[16] = 128;
            }
            if (current.IsToggled(Keys.Capital))
            {
                nativeState[20] = 1;
            }
            switch (KeyboardNativeMethods.ToUnicodeEx(virtualKeyCode, scanCode, nativeState, stringBuilder, stringBuilder.Capacity, fuState, dwhkl))
            {
            case -1:
                flag = true;
                KeyboardNativeMethods.ClearKeyboardBuffer(virtualKeyCode, scanCode, dwhkl);
                chars = null;
                break;

            case 0:
                chars = null;
                break;

            case 1:
                if (stringBuilder.Length > 0)
                {
                    chars = new char[]
                    {
                        stringBuilder[0]
                    };
                }
                else
                {
                    chars = null;
                }
                break;

            default:
                if (stringBuilder.Length > 1)
                {
                    chars = new char[]
                    {
                        stringBuilder[0],
                        stringBuilder[1]
                    };
                }
                else
                {
                    chars = new char[]
                    {
                        stringBuilder[0]
                    };
                }
                break;
            }
            if (KeyboardNativeMethods.lastVirtualKeyCode != 0 && KeyboardNativeMethods.lastIsDead)
            {
                if (chars != null)
                {
                    StringBuilder stringBuilder2 = new StringBuilder(5);
                    KeyboardNativeMethods.ToUnicodeEx(KeyboardNativeMethods.lastVirtualKeyCode, KeyboardNativeMethods.lastScanCode, KeyboardNativeMethods.lastKeyState, stringBuilder2, stringBuilder2.Capacity, 0, dwhkl);
                    KeyboardNativeMethods.lastIsDead         = false;
                    KeyboardNativeMethods.lastVirtualKeyCode = 0;
                }
                return;
            }
            KeyboardNativeMethods.lastScanCode       = scanCode;
            KeyboardNativeMethods.lastVirtualKeyCode = virtualKeyCode;
            KeyboardNativeMethods.lastIsDead         = flag;
            KeyboardNativeMethods.lastKeyState       = (byte[])nativeState.Clone();
        }
        public static bool isWinKeyPressed(this KeyEventArgsExt _)
        {
            KeyboardState keyboardState = KeyboardState.GetCurrent();

            return(keyboardState.IsDown(Keys.LWin) || keyboardState.IsDown(Keys.RWin));
        }