I am struggling a bit here. Appreciate any help possible. I am trying to come up with something that does the following:
- Via a keyboard hook, check whenever a key is pressed, even while app not in focus.
- If just one key was pressed then the press goes through and the text is typed on the screen: Ex: "A" > "A"
- 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!!