Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

c# - console application wait for keyup

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:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...