2

I am struggling a bit here. Appreciate any help possible. I am trying to come up with something that does the following:

  1. Via a keyboard hook, check whenever a key is pressed, even while app not in focus.
  2. If just one key was pressed then the press goes through and the text is typed on the screen: Ex: "A" > "A"
  3. If 2 or more keys are pressed at the same time, then e.handled = true and the presses are not actually typed out, but converted into something else: EX: "AB" > "Shift"

Currently my issue is that since gkh_KeyDown fires once per key, by the time the second key is pressed the first one will already have been typed out. Here is the code I currently have:

        List<Keys> KeysPressed = new List<Keys>();
        List<KeyEventArgs> KeyEvents = new List<KeyEventArgs>();

        async void gkh_KeyDown(object sender, KeyEventArgs e)
        {
            KeysPressed.Add(e.KeyCode);
            KeyEvents.Add(e);

            //wait x milliseconds to see if other key is pressed
            await Task.Delay(100);

            if (KeysPressed.Count != 1)
                for (int i = 0; i < KeyEvents.Count; i++)
                    KeyEvents[i].Handled = true;

        }

        void gkh_KeyUp(object sender, KeyEventArgs e)
        {
            //create string out of all keys pressed
            for (int i = 0; i < KeysPressed.Count; i++)
                KeysPressedString += KeysPressed[i];

            //log full string
            if (KeysPressedString.Length == KeysPressed.Count && KeysPressed.Count >= 2)
                    Console.WriteLine(KeysPressedString);

            //reset valuess
            KeysPressedString = "";
            KeysPressed.Clear();
            KeyEvents.Clear();
        }

I tried implementing a small delay before actually enabling/disabling the keypress, but so far I've only been able to disable from the second character onward. The "A" in "AB" is always typed out.

Hopefully I made some sense. Since I am not using any microcontroller, and this is all supposed to work with a regular keyboard; I am not sure if QMK would be an option here or not.

Thank you!!

  • Maybe look at [this answer](https://stackoverflow.com/questions/18291448/how-do-i-detect-keypress-while-not-focused). – ourmandave Jul 18 '21 at 04:04
  • Why would you remap keys out from under another application, which is what it looks like if another application has focus. What are you trying to do, exactly? If you're trying to compensate for the irrational lack of important text-editing keys on a Chromebook, please, let me help! I need my Home, End, PageUp, and PageDown (not to mention CapsLock!) – MicroservicesOnDDD Jul 18 '21 at 17:24
  • To quote relevant CodeProject article [**Global System Hooks in .NET**](https://www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET), "System hooks are powerful. And, with that power comes responsibility. When something goes wrong with system hooks, they don't just break your application. **They can break every application running on your system.** It is unlikely that it would actually come to that extreme. Nonetheless, you need to double and triple check your code when using system hooks." (Change in emphasis mine.) I sincerely wish you good luck. – MicroservicesOnDDD Jul 18 '21 at 17:47

0 Answers0