I'm trying to create a basic program for now which displays live inputs as they are typed. An example output would be something like this:
Current inputs: CTRL ALT S V SPACE
(Note that these would be the currently held keys. If the user released the CTRL key, that input would disappear).
I created some sample code here. It's quite crude, but it's my best attempt at this. It's inside a larger program (which I use as a sandbox), but the only context that should be needed is that using System;
is at the top of the program
public static void KeypressTest()
{
char[] keys = new char[10];
int x;
while (1==1)
{
for (x = 0; x < 10; x++) //All the loops attempt to fill the array if possible. I figured this was the easiest way to store multiple characters at once
{
keys[x] = Convert.ToChar(ConsoleKey.MediaStop); //I don't know how to set it to blank, so I picked a character that isn't likely to be used
}
for (x = 0; x < 10; x++)
{
if (keys[x] != Convert.ToChar(ConsoleKey.MediaStop)) { x += 1; } //This skips the key if it is not the default (MediaStop). This is temporary and will be altered once I can figure out how to register a key has been lifted
else { keys[x] = Console.ReadKey().KeyChar; }
}
Console.Write("\rCurrent inputs: \n");
for (x = 0; x < 10; x++)
{ Console.Write(Convert.ToString(keys[x])); }
Console.WriteLine();
}
}
The code itself has the problem of waiting for all 10 inputs, then displaying the keys typed, and ignoring the release of keys. It seems to act more like a keylogger than the target of showing currently held keys. Example output would look like this, if the user types "hello everyone!!!!!":
Current inputs:
hello ever
Current inputs:
yone!!!!!
The target would show each key for the instant it is held, and would not show it once released (but I have no idea how to go about this).
The problem is, I'm quite new to C# and haven't found a way to update the inputs as they are typed (I am aware of Console.ReadKey() but I don't know how to make it run in the background. I know what threading is but have no knowledge on how to do it, so any tips would be appreciated. Thanks!