2017-04-22 3 views
0

私はその質問にどのように言いたいのか分かりませんでした...割り当てのためのキー押下待ち行列はありますか?

私のアプリケーションでは、ConsoleKeyInfoオブジェクトがConsole.ReadKey()に割り当てられる前にユーザーがEnterキーを押すと、ユーザーが押す。実際には、ユーザーが5回入力すると自動的に5回割り当てられ、これは私にとっては非常に悪いため、スタックされているようです。私はこれを非常に慎重にデバッグプロセスを通して辿ってきましたが、これが起こることはかなり確信しています。 Thread.Sleep()を使用して、ユーザーがEnterキーを何度も押すことができる機会のウィンドウが作成されます。私はそれを使用することに警告されたが、私はそれがユーザーのテキストの行を読むことができるように私はちょうど停止するためにすべてが必要なので、私にとってはうまくいくと思った。私はそのような機会が存在するとは思わないかもしれないと思っていますか?または、私はここで間違って起こっていることを解釈しているのでしょうか?私はアプリケーション全体で1行のコードの入力を求めるだけです...

答えて

0

あなたが正しく言うことを読んでいるなら、押されたときのキー押下がすべてキューイングされ、次にあなたがConsole.Readkeyを実行したときに削除され...これは、あなたが近接して繰り返しを取得するときに問題を引き起こします。

以下は、必要に応じてキー入力キューを処理する方法です。あなたが「オプション」の選択やフリーテキストの入力などのためにキーボードを使用しているのかどうかはわかりませんが、それは助けになると思います。

こちらをご覧ください:

を、私はそれが「繰り返し」キー押下を検出しようとするようにすることを適応してきた、そしてそれは「それらを無視」/「それらを食べる」しますそれらが一定の時間間隔内に一緒に発生する場合....しかし、それは別のキーを押す場合は、移動します。

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

namespace ConsoleApp6 
{ 
    class Program 
    { 
     public static void Main() 
     { 
      ConsoleKeyInfo ckilast = default(ConsoleKeyInfo); 
      ConsoleKeyInfo ckicurrent = default(ConsoleKeyInfo); 

      Console.WriteLine("\nPress a key to display; press the 'x' key to quit."); 

      // Adjust this "window" period, till it feels right 

      //TimeSpan tsignorerepeatedkeypresstimeperiod = new TimeSpan(0, 0, 0, 0, 0); // 0 for no extra delay 

      TimeSpan tsignorerepeatedkeypresstimeperiod = new TimeSpan(0, 0, 0, 0, 250); 

      do 
      { 
       while (Console.KeyAvailable == false) 
        Thread.Sleep(250); // Loop until input is entered. 

       ckilast = default(ConsoleKeyInfo); 

       DateTime eatingendtime = DateTime.UtcNow.Add(tsignorerepeatedkeypresstimeperiod); // will ignore any "repeated" keypresses of the same key in the repeat window 

       do 
       { 
        while (Console.KeyAvailable == true) 
        { 
         ckicurrent = Console.ReadKey(true); 

         if (ckicurrent.Key == ConsoleKey.X) 
          break; 

         if (ckicurrent != ckilast) // different key has been pressed to last time, so let it get handled 
         { 
          eatingendtime = DateTime.UtcNow.Add(tsignorerepeatedkeypresstimeperiod); // reset window period 

          Console.WriteLine("You pressed the '{0}' key.", ckicurrent.Key); 

          ckilast = ckicurrent; 

          continue; 
         } 
        } 

        if (Console.KeyAvailable == false) 
        { 
         Thread.Sleep(50); 
        } 

       } while (DateTime.UtcNow < eatingendtime); 

      } while (ckicurrent.Key != ConsoleKey.X); 
     } 
    } 
} 
関連する問題