gpt4 book ai didi

c# - 控制台应用程序等待 keyup

转载 作者:行者123 更新时间:2023-11-30 17:06:02 26 4
gpt4 key购买 nike

我目前正在使用 C# 开发 roguelike。世界、玩家等在控制台内呈现。但是当控制台中的变化太多时,它会滞后。为了绕过这个,我试图让程序等待玩家取消按下玩家正在按下的键。任何人都知道如何制作这个?糟糕,没有像 Console.ReadKeyUp() 这样的东西。

while(Console.KeyAvailable){
}

好像不行...

这里有一些代码:

public void move(){
if(!MainClass.loading){
switch(Console.ReadKey().Key){
case ConsoleKey.NumPad8:
//walk up
break;
case ConsoleKey.NumPad4:
//walk left
break;
case ConsoleKey.NumPad6:
//walk right
break;
case ConsoleKey.NumPad2:
//walk down
break;
case ConsoleKey.NumPad7:
//walk left up
break;
case ConsoleKey.NumPad9:
//walk right up
break;
case ConsoleKey.NumPad1:
//walk left down
break;
case ConsoleKey.NumPad3:
//walk right down
break;
case ConsoleKey.NumPad5:
//eat
break;
}

}

}

这是它的样子:

enter image description here

最佳答案

如果不将此应用程序重构为事件驱动格式(带有消息循环的隐藏窗口等),您最好的选择可能是深入了解 WinAPI 中可用的各种功能。这样的事情可能会起作用:

 [DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetKeyboardState(byte [] lpKeyState);

您可以使用此函数查询完整的键盘状态 - 它返回一个包含键盘状态的 256 字节数组。请参阅:here for more和一些例子。

例如,您也可以使用 Console.ReadKey() 然后阻塞直到 GetKeyState() 返回低位 0对于有问题的关键:

 [DllImport("user32.dll")]
static extern short GetKeyState(VirtualKeyStates nVirtKey);

参见:here for more和例子。

MSDN:GetKeyState ; GetKeyboardState

关于c# - 控制台应用程序等待 keyup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com