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. Checking for a keypress in console.

Checking for a keypress in console.

Scheduled Pinned Locked Moved C#
help
6 Posts 2 Posters 0 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.
  • J Offline
    J Offline
    jblouir
    wrote on last edited by
    #1

    I want to have a loop that checks if a key has been pressed during that time. The loop has a delay of Time.Sleep(25); So the loop starts, check if a key is down then continues with the code, the time delay hits then it repeats. The problem is I can't use ReadKey Read or ReadLine because these all stop the "flow" of the code causing it to physically halt, I want to code(loop) to continue if a key is pressed or not. And System.Windows.Forms; isnt an option as far as I know. Thanks guys.

    N 1 Reply Last reply
    0
    • J jblouir

      I want to have a loop that checks if a key has been pressed during that time. The loop has a delay of Time.Sleep(25); So the loop starts, check if a key is down then continues with the code, the time delay hits then it repeats. The problem is I can't use ReadKey Read or ReadLine because these all stop the "flow" of the code causing it to physically halt, I want to code(loop) to continue if a key is pressed or not. And System.Windows.Forms; isnt an option as far as I know. Thanks guys.

      N Offline
      N Offline
      Nissim Salomon
      wrote on last edited by
      #2

      Hi try using different thread in order to check if a key was pressed for example static void Main(string[] args) { object key = new object(); bool stopLoop = false; Thread t1 = new Thread(delegate() { while (Console.ReadLine() != "Q") ; lock (key) { stopLoop = true; } Console.WriteLine("Stop Command Issued"); }); t1.Start(); while (true) { lock(key) { if (stopLoop == true) break; } } }

      J 1 Reply Last reply
      0
      • N Nissim Salomon

        Hi try using different thread in order to check if a key was pressed for example static void Main(string[] args) { object key = new object(); bool stopLoop = false; Thread t1 = new Thread(delegate() { while (Console.ReadLine() != "Q") ; lock (key) { stopLoop = true; } Console.WriteLine("Stop Command Issued"); }); t1.Start(); while (true) { lock(key) { if (stopLoop == true) break; } } }

        J Offline
        J Offline
        jblouir
        wrote on last edited by
        #3

        Wow thanks! This is great, but is there anyway to use ConsoleKeyInfo .key ConsoleKey etc instead? Thanks in advance ^0^

        J 1 Reply Last reply
        0
        • J jblouir

          Wow thanks! This is great, but is there anyway to use ConsoleKeyInfo .key ConsoleKey etc instead? Thanks in advance ^0^

          J Offline
          J Offline
          jblouir
          wrote on last edited by
          #4

          Nevermind I figured it out on my own, all I had to do was change/add these two lines. ^O^. Thanks! ConsoleKeyInfo keyInput = new ConsoleKeyInfo(); while (Console.ReadKey().Key != ConsoleKey.Escape) ;

          J 1 Reply Last reply
          0
          • J jblouir

            Nevermind I figured it out on my own, all I had to do was change/add these two lines. ^O^. Thanks! ConsoleKeyInfo keyInput = new ConsoleKeyInfo(); while (Console.ReadKey().Key != ConsoleKey.Escape) ;

            J Offline
            J Offline
            jblouir
            wrote on last edited by
            #5

            Ran into a slight problem. This performs actions within the while without stopping to check for the key but how do you add additional keys? So the loop goes constantly checking for an escape key press, and if it happens it breaks the loop. but what if I want it to perform actions based on other keys while its looping? If you got any info that would be great. =O) -- modified at 16:12 Monday 18th June, 2007 Don't worry again I think I cracked it.

            J 1 Reply Last reply
            0
            • J jblouir

              Ran into a slight problem. This performs actions within the while without stopping to check for the key but how do you add additional keys? So the loop goes constantly checking for an escape key press, and if it happens it breaks the loop. but what if I want it to perform actions based on other keys while its looping? If you got any info that would be great. =O) -- modified at 16:12 Monday 18th June, 2007 Don't worry again I think I cracked it.

              J Offline
              J Offline
              jblouir
              wrote on last edited by
              #6

              If anyone is interested in how this is done. I basically modified it so that it reads the key in the thread inside the loop over and over, and it does things according to whatever the key is. This is just an example. static void Main(string[] args) { object key = new object(); bool stopLoop = false; ConsoleKeyInfo keyInput = new ConsoleKeyInfo(); Thread t1 = new Thread(delegate() { while (keyInput.Key != ConsoleKey.Escape) // repeats the below code until... { keyInput = Console.ReadKey(); // Constantly checks for pressed key if (keyInput.Key == ConsoleKey.UpArrow) // does things depending on key { Console.WriteLine("Up Arrow"); } if (keyInput.Key == ConsoleKey.Escape) // the loop break { lock (key) { stopLoop = true; } } } }); t1.Start(); while (true) { Thread.Sleep(200); // Putting a delay in so it doesnt run at a billion mph Console.Write("01"); // Writing numbers just to visually check the speed lock (key) // This stops the loop if Escape is pressed as defined above. { if (stopLoop == true) break; } } } } }

              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