控制台应用程序等待keyup

控制台应用程序等待keyup

本文介绍了控制台应用程序等待keyup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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:
// left left
break;
case ConsoleKey.NumPad6:
// walk right
break;
case ConsoleKey.NumPad2:
// walk down
break;
case ConsoleKey.NumPad7:
//向左走
break;
case ConsoleKey.NumPad9:
//向右上走
break;
case ConsoleKey.NumPad1:
//向左走
break;
case ConsoleKey.NumPad3:
//向右下走
break;
case ConsoleKey.NumPad5:
//吃
break;
}

}

}

下面是它的样子:



解决方案

没有重构这个应用程序成一个事件驱动的格式bet可能是通过WinAPI中可用的各种功能。这样的东西可能工作:

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

您可以使用此函数查询完整键盘状态 - 它返回一个256字节的数组,键盘状态。请参阅:以及一些示例。



例如,您可以使用 Console.ReadKey(),然后阻止,直到 GetKeyState()为问题的键返回低位 0

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

请参阅:和示例。



MSDN:; p>

I´m currently working on a roguelike in c#.The world, player etc. are rendered inside of a console. But when there are too many changes in the console, it lags.To bypass this, I´m trying to let programm wait for the player to un-press the key the player is pressing.Anyone has an idea on how to make this?Bad, that there isn´t something like Console.ReadKeyUp().

while(Console.KeyAvailable){
}

seems not to work...

Here´s a bit of code:

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;
                }

            }

        }

And here's what it looks like:

解决方案

Without refactoring this application into an event-driven format (hidden window with message loop, etc) your best bet is probably to dig through the various functions available in the WinAPI. Something like this may work:

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

You can use this function to query the full keyboard state - it returns a 256-byte array containing the state of the keyboard. See : here for more and some examples.

You might also, for example, use Console.ReadKey() and then block until GetKeyState() returns a low-order 0 for the key in question :

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

See : here for more and examples.

MSDN : GetKeyState; GetKeyboardState

这篇关于控制台应用程序等待keyup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:35