11 Replies - 198 Views - Last Post: 31 May 2019 - 08:10 AM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Console.readkey(true)

Posted 31 May 2019 - 05:18 AM

I have forgotten some of my programming skills so I need to update and it is hard to find the answer online me at Microsofts page so i ask here.

I would like to make an while loop and to exit from this loop of I press number 5.

But most important is that my input on the keyboard gets read By console application. I don't want to make an if statement because then I need to press enter and that is nooby.

I remember it goes something like "
"Console.readkey(true)


Something and I press the numbers and it gets written and read directly.

Also it must be in while loop correct? So that it exists when I press the specific button

Is This A Good Question/Topic? 0
  • +

Replies To: Console.readkey(true)

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15107
  • View blog
  • Posts: 60,427
  • Joined: 12-June 08

Re: Console.readkey(true)

Posted 31 May 2019 - 05:36 AM

Please post what you have tried.

It maybe worth your time to pick up a book on the topic and refresh yourself with the basics.
Was This Post Helpful? 0
  • +
  • -

#3 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Console.readkey(true)

Posted 31 May 2019 - 07:20 AM

This is the only thing written so far. I am looking to find this exactly one. You know which one this is right?

If keychar =='5'

What is this Keynote?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15107
  • View blog
  • Posts: 60,427
  • Joined: 12-June 08

Re: Console.readkey(true)

Posted 31 May 2019 - 07:23 AM

Okay.. how about some pseudo code then?

Usually chucking 'msdn' in front of things helps narrow down google's search to those help pages.

Example: msdn c# console read key


Quote

What is this Keynote?

"Keynote"?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6966
  • View blog
  • Posts: 23,676
  • Joined: 05-May 12

Re: Console.readkey(true)

Posted 31 May 2019 - 07:28 AM

Did you try playing with the sample code which comes with the documentation to deepen your learning about Console.ReadKey()
Was This Post Helpful? 0
  • +
  • -

#6 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Console.readkey(true)

Posted 31 May 2019 - 07:28 AM

while not 1 repeat
If 1 type title
If 2 type author
If 3 type genre
If 4 exit


I need the one that reads the keyboard directly I think it is console.readkeychar
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15107
  • View blog
  • Posts: 60,427
  • Joined: 12-June 08

Re: Console.readkey(true)

Posted 31 May 2019 - 07:31 AM

Piggy backing on skydiver's comment.. yeah.. there are quite a few methods available to the 'console' name space... read, read key, read line, clear, beep, etc.


https://docs.microso...etframework-4.8
Was This Post Helpful? 0
  • +
  • -

#8 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Console.readkey(true)

Posted 31 May 2019 - 07:42 AM

var input = Console.ReadKey();

switch (input.Key) //Switch on Key enum
{
    case ConsoleKey.X:
        break;
    case ConsoleKey.Y:
        break;
}


So far I have come this far. If I want to use digits instead how do I make it read digit's?
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15107
  • View blog
  • Posts: 60,427
  • Joined: 12-June 08

Re: Console.readkey(true)

Posted 31 May 2019 - 07:44 AM

You would need to convert the text into an integer. All input comes in as a string.

msdn c# convert to int
Was This Post Helpful? 0
  • +
  • -

#10 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Console.readkey(true)

Posted 31 May 2019 - 07:52 AM

I did this but does not work

int32.tryparse(input,out int p);


"Cannot convert from 'system.condolekeyinfo to dtring'
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15107
  • View blog
  • Posts: 60,427
  • Joined: 12-June 08

Re: Console.readkey(true)

Posted 31 May 2019 - 07:57 AM

Perhaps switch to read line where you are getting a string input... or just "read".

Alternatively you could go the super long route to convert 'tostring()' the ConsoleKey object to get the string value and then try and convert that. I would probably not go that route.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6966
  • View blog
  • Posts: 23,676
  • Joined: 05-May 12

Re: Console.readkey(true)

Posted 31 May 2019 - 08:10 AM

You'll want to check against ConsoleKey.D0 through ConsoleKey.D9 for the numbers at the top row of the keyboard or ConsoleKey.NumPad0 through ConsoleKey.NumPad9. See ConsoleKey documentation.

As a quick aside, on top of your current shotgun approach to learning C#, using Intellisense is really a poor way to explore the framework, unless you at least do a cursory scan through the documentation to get a feel for what to expect when Intellisense kicks in.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1