控制台阻止所有用户输入不是上/下箭头键?

时间:2014-09-29 20:59:09

标签: c#

嘿伙计们这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test3menu
{
    class Program
    {
        public static string[] stringArray = new string[3] { "Menu", "Options", "Exit" };
        public static int pointerLocation = 0;

        public static void printMenu(int pointerLoc)
        {
            if (stringArray.Length >= pointerLoc && pointerLoc >= 0)
            {
                for (int i = 0; i < stringArray.Length; i++)
                {
                    string toWrite = "";
                    if (i == pointerLoc) toWrite = "> ";
                    toWrite += stringArray[i];
                    Console.WriteLine(toWrite);


                }
            }
        }

        static void Main(string[] args)
        {
            printMenu(0);
            while (true)
            {
                ConsoleKey key = Console.ReadKey(true).Key;
                Console.Clear();
                if (key == ConsoleKey.DownArrow)
                {
                    if (pointerLocation < stringArray.Length - 1)
                    {
                        pointerLocation += 1;
                    }
                    printMenu(pointerLocation);
                }
                else if (key == ConsoleKey.UpArrow)
                {
                    if (pointerLocation > 0)
                    {
                        pointerLocation -= 1;
                    }

                }
            }
        }
    }
}

现在,如果您尝试运行它,当我按下X时,屏幕将变黑。我已经尝试过if语句来检查key!= ConsoleKey.UpArrow / DownArrow然后再次打印菜单,但它不起作用。

那么我该如何限制用户输入呢?

2 个答案:

答案 0 :(得分:0)

我在代码中看到了2个问题

1-无论按下什么键,控制台总是被清除

2-您仅针对DownArrow而不是UpArrow

调用printMenu(pointerLocation);

我认为这应该有效:

    static void Main(string[] args)
    {
        printMenu(0);
        while (true)
        {
            ConsoleKey key = Console.ReadKey(true).Key;

            if (key != ConsoleKey.DownArrow && key != ConsoleKey.UpArrow)
                continue;


            switch (key)
            {
                case ConsoleKey.DownArrow:
                    if (pointerLocation < stringArray.Length - 1)
                    {
                        pointerLocation += 1;
                    }
                    break;
                case ConsoleKey.UpArrow:
                    if (pointerLocation > 0)
                    {
                        pointerLocation -= 1;
                    }
                    break;
            }

            Console.Clear();
            printMenu(pointerLocation);
        }
    }

答案 1 :(得分:0)

你可以按照以下方式编写方法:

static IEnumerable<ConsoleKey> ReadKeyStrokes()
{
  while ( true )
  {
    ConsoleKeyInfo cki = Console.ReadKey(true) ; // get the keystroke, but don't echo it to the console.

    switch( cki.Key )
    {
    case ConsoleKey.LeftArrow  :
    case ConsoleKey.RightArrow :
    case ConsoleKey.DownArrow  :
    case ConsoleKey.UpArrow    : yield return cki.Key ; break ;
    default                    : Console.Beep()       ; break ;
    }
  }
}

然后你会说这些话:

static int Main( string[] args )
{
  Console.WriteLine("Press an arrow key." ) ;
  Console.Write("? " ) ;
  foreach ( ConsoleKey key in ReadKeyStrokes() )
  {
    Console.WriteLine( "You pressed {0}" , key ) ;
    Console.Write("? ") ;
  }
  return 0 ;
}

Edite to note:您可能会发现一种方法来刷新任何未处理的击键的键盘缓冲区:

static void FlushKeyBoardBuffer()
{
  while ( Console.KeyAvailable )
  {
    Console.ReadKey() ;
  }
  return ;
}