Inputs other than read.line | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Inputs other than read.line

how do I get the program to check if a key is pressed? ie, it doesn't wait for the enter key to be pressed before it continues. for example, a counter that keeps counting up, until "z" is pressed, then counts down until "x" is pressed.

16th Jan 2017, 3:17 AM
Tristan
8 Answers
+ 3
Is this a console program or WinForm? You can check for keypressed events.
16th Jan 2017, 3:49 AM
Alex
Alex - avatar
+ 3
This can get tricky as you would either need to use pinvokes to get global system keys because console applications doesn't support keypressed events as you need them or you could run the main logic in a background thread and read the users input from the console readkey. There is a lot of way to go about this but no simple ones unless you want to output the game one line at a time. There is a option to do it with a WinForm app where you can use pinvokes to allocate a console window then you handle keypressed events in the WinForm while outputting the game into the console.
16th Jan 2017, 4:12 AM
Alex
Alex - avatar
+ 3
For threading you can read this but it might be steep if you are a beginner https://msdn.microsoft.com/en-us/library/aa645740 Yes you can read data streams but you would have to get the data from the windows api which again can require pinvokes luckily C# has the ability to get data from IO ports using System.IO.Ports see here https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx
16th Jan 2017, 4:31 AM
Alex
Alex - avatar
+ 2
Console.ReadKey();if your are using console. or like Alex said keypressed event if you are using winforms.
16th Jan 2017, 5:06 AM
Ihab Taleb
Ihab Taleb - avatar
0
At the moment its just the console version. it has been 20 years since I programmed anything, and the last language I used was QBASIC. What I am actually wanting to do is program a simple game that was part of a "learn BASIC" book I had for the C64, called "Death Vally". it basically printed X V X on the screen, each time it printed a line, it would randomly move the Xs left or right, the player could move the V left or right by pressing z or x, if the V collided with an X it was game over. As you played it, the screen would look like this... X V X X V X X V X X V X X V X X V X X V X X V X X V X X V X X V X X V X
16th Jan 2017, 4:01 AM
Tristan
0
so are threads kind of like "subprograms" that run along side, or do i have to run two seperate programs at the same time?
16th Jan 2017, 4:21 AM
Tristan
0
what about data streams, ie, input from a micro-controller? can C# in console periodically read on/off values through USB or RS232 and print a boolean response on the screen?
16th Jan 2017, 4:25 AM
Tristan
0
Thankyou. I'll have a look at that.
16th Jan 2017, 4:33 AM
Tristan