我已经改变了我的答案,这更接近你想要的。我在这个线程(NativeKeyboard 内部类)中找到了一个 keydown 事件实现:
C# arrow key input for a console app
解决方案在于多个线程计时器一起检查值或条件。主程序线程处于循环中,直到满足退出条件(按 ESC 键)。有一个提示器计时器,它打印表示按下的键列表的值的缓存,一行一行地用空格清除。对于每个 ConsoleKey 枚举,都会创建一个计时器并映射 timer-key duo。每当按下某个键时,该键的映射计时器将能够确定按下了该特定键并使用键的值/字符串更新列表。每当释放键时,列表也会更新为该键的空字符串值。
我必须添加 LocalModifiers 枚举作为 ALT、CTRL、SHIFT 和 CAPS LOCK 的键码。
我必须使用 lock() 来确保所有这些计时器都可以工作而不会产生并发问题。
当我执行代码时,它的输出与您描述的非常接近。有时在按下 7-8 个键的情况下,一些按下的键不显示。
这是我的代码:
class Program
{
public static Dictionary<int, String> inputMap = new Dictionary<int, string>();
public static Dictionary<Timer, ConsoleKey> TimerKeyMap = new Dictionary<Timer, ConsoleKey>();
public static Dictionary<Timer, LocalModifiers> TimerModifierMap = new Dictionary<Timer, LocalModifiers>();
public static bool continueLoop = true;
public static object locker = new object();
static void Main(string[] args)
{
Program program = new Program();
program.run();
}
public enum LocalModifiers :int
{
SHIFT = 0x10,
CTL = 0x11,
ALT = 0x12,
CAPS_LOCK = 0x14
}
public void run()
{
Timer keyPressedPrompter = new Timer();
keyPressedPrompter.Interval = 60;
keyPressedPrompter.Elapsed += new ElapsedEventHandler(KeyPressedPrompterEvent);
keyPressedPrompter.Enabled = true;
foreach (ConsoleKey key in Enum.GetValues(typeof(ConsoleKey)))
{
Timer timer = new Timer();
TimerKeyMap[timer] = key;
timer.Interval = 60;
timer.Elapsed += new ElapsedEventHandler(KeyPressedCheckerEvent);
timer.Enabled = true;
}
foreach (LocalModifiers key in Enum.GetValues(typeof(LocalModifiers)))
{
Timer timer = new Timer();
TimerModifierMap[timer] = key;
timer.Interval = 60;
timer.Elapsed += new ElapsedEventHandler(ModifierPressedCheckerEvent);
timer.Enabled = true;
}
Console.WriteLine("Current inputs:");
while (continueLoop) { }
}
public static void ModifierPressedCheckerEvent(object source, EventArgs e)
{
lock (locker)
{
if (NativeKeyboard.IsKeyDown((int)TimerModifierMap[(Timer)source]))
{
//Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString()+ " pressed");
inputMap[(int)TimerModifierMap[(Timer)source]] = TimerModifierMap[(Timer)source].ToString() + " ";
}
else
{
// Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString() + " released");
inputMap[(int)TimerModifierMap[(Timer)source]] = "";
}
}
}
public static void KeyPressedCheckerEvent(object source, EventArgs e)
{
lock (locker)
{
if (NativeKeyboard.IsKeyDown((int)TimerKeyMap[(Timer)source]))
{
if (TimerKeyMap[(Timer)source] == ConsoleKey.Escape)
continueLoop = false;
//Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString()+ " pressed");
inputMap[(int)TimerKeyMap[(Timer)source]] = TimerKeyMap[(Timer)source].ToString() + " ";
}
else
{
// Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString() + " released");
inputMap[(int)TimerKeyMap[(Timer)source]] = "";
}
}
}
public static void KeyPressedPrompterEvent(object source, EventArgs e)
{
Console.Write(" ");//clear the line - - can be extended
Console.Write("\r");
lock (locker)
{
foreach (KeyValuePair<int, String> entry in inputMap)
{
// do something with entry.Value or entry.Key
Console.Write(entry.Value);
}
}
}
}
/// <summary>
/// Provides keyboard access.
/// </summary>
internal static class NativeKeyboard
{
/// <summary>
/// A positional bit flag indicating the part of a key state denoting
/// key pressed.
/// </summary>
private const int KeyPressed = 0x8000;
/// <summary>
/// Returns a value indicating if a given key is pressed.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>
/// <c>true</c> if the key is pressed, otherwise <c>false</c>.
/// </returns>
public static bool IsKeyDown(int key)
{
return (GetKeyState((int)key) & KeyPressed) != 0;
}
/// <summary>
/// Gets the key state of a key.
/// </summary>
/// <param name="key">Virtuak-key code for key.</param>
/// <returns>The state of the key.</returns>
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern short GetKeyState(int key);
}