Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How I can Check keyboard buffer?

How I can Check keyboard buffer?

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nikulin_andrey
    wrote on last edited by
    #1

    Hi! Please help me! I hava a problem. How I can check Keyboard buffer is empty or not? For instance do { // do something } while (CharFromKeyBoard != "q"); System.Read and SystemReadLine waiting user input, but I need do some operation until user press some key, for instance char "q". Sorry my English.

    H S 2 Replies Last reply
    0
    • N nikulin_andrey

      Hi! Please help me! I hava a problem. How I can check Keyboard buffer is empty or not? For instance do { // do something } while (CharFromKeyBoard != "q"); System.Read and SystemReadLine waiting user input, but I need do some operation until user press some key, for instance char "q". Sorry my English.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Try something like this (especially StreamReader.Peek()):

      StreamReader reader = new StreamReader(Console.In);
      try
      {
      reader = new StreamReader(Console.In); // Or whatever Stream.
      do
      {
      // Do something.
      } while (reader.Peek() != 113); // Might want to check 81, too.
      }
      finally
      {
      if (reader != null)
      reader.Close();
      }


      Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN

      N 1 Reply Last reply
      0
      • H Heath Stewart

        Try something like this (especially StreamReader.Peek()):

        StreamReader reader = new StreamReader(Console.In);
        try
        {
        reader = new StreamReader(Console.In); // Or whatever Stream.
        do
        {
        // Do something.
        } while (reader.Peek() != 113); // Might want to check 81, too.
        }
        finally
        {
        if (reader != null)
        reader.Close();
        }


        Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN

        N Offline
        N Offline
        nikulin_andrey
        wrote on last edited by
        #3

        This sample doesn't work. The best overloaded method match for 'System.IO.StreamReader.StreamReader(System.IO.Stream)' has some invalid arguments Argument '1': cannot convert from 'System.IO.TextReader' to 'System.IO.Stream' :zzz:

        H 1 Reply Last reply
        0
        • N nikulin_andrey

          This sample doesn't work. The best overloaded method match for 'System.IO.StreamReader.StreamReader(System.IO.Stream)' has some invalid arguments Argument '1': cannot convert from 'System.IO.TextReader' to 'System.IO.Stream' :zzz:

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          That exception message is self-explanitory - you should be able to figure that one out. I just threw this sample together quick, but you have to learn to research problems yourself or you're really in trouble. If Console.In is a TextReader, then take a look at its methods or StreamReader's constructors. The former yields that TextReader.Peek does exist so you don't even have to create a new class instance. Just use Console.In.Peek to look at the next character in the buffer. Researching the docs is at least half of what development is.


          Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN

          1 Reply Last reply
          0
          • N nikulin_andrey

            Hi! Please help me! I hava a problem. How I can check Keyboard buffer is empty or not? For instance do { // do something } while (CharFromKeyBoard != "q"); System.Read and SystemReadLine waiting user input, but I need do some operation until user press some key, for instance char "q". Sorry my English.

            S Offline
            S Offline
            scarecrow900
            wrote on last edited by
            #5

            I know this is some years late but you might still be interested. do { } while(!Console.KeyAvailable) or do { } while(!Console.KeyAvailable && Console.ReadKey().KeyChar == 'q')

            Thats the sound of Inevitability ....

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups