5 Replies - 890 Views - Last Post: 10 December 2013 - 12:41 AM Rate Topic: -----

#1 Bree11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 13

How to read integers from command line without having to press enter?

Posted 07 December 2013 - 12:38 PM

Hey :>
How can I to scanf a single integer so while the running the user could enter any single number (as 1) without having to press enter, and the program still will continue in the process run?
for example (to be more clearly): on the screen appears a message "Please enter an integer", I pressing just 1, and it immediately sent me a new message "Well done!", without that I pressed enter.
Is This A Good Question/Topic? 0
  • +

Replies To: How to read integers from command line without having to press enter?

#2 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: How to read integers from command line without having to press enter?

Posted 07 December 2013 - 01:01 PM

It isn't easy. Read this.
Was This Post Helpful? 1
  • +
  • -

#3 Bree11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 13

Re: How to read integers from command line without having to press enter?

Posted 09 December 2013 - 12:20 PM

Hey :>>
I found another way :> so for someone that had the same problem:
int num;
num=getch()-48; //48 represent the distance between the ASCII value and the number that the user meant.
printf("%d", num); //if you want that the function would show you the number you pressed
//and you can continue whit the code :> but this is just for integers


Was This Post Helpful? 0
  • +
  • -

#4 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to read integers from command line without having to press enter?

Posted 09 December 2013 - 12:25 PM

I've renamed your thread. "question" is not a good enough thread title. Please be descriptive; name it in such a way that it actually tells people what the question is about.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6499
  • View blog
  • Posts: 22,244
  • Joined: 05-May 12

Re: How to read integers from command line without having to press enter?

Posted 09 December 2013 - 11:19 PM

getch() is non-standard C. Some compilers don't support that. Which brings you right back to post #2 again.
Was This Post Helpful? 0
  • +
  • -

#6 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: How to read integers from command line without having to press enter?

Posted 10 December 2013 - 12:41 AM

The easiest way to do this, is to use a library or header file. I have used conio.h with
int mychar;
if( kbhit() ) {
  mychar=getch();
  printf("%c\n",mychar); 
}



The int data type always looks weird, but it works correctly this way. If you want to read the arrow keys however, you need to call getch() twice, since they send two chars when pressed. You can't detect ctrl + c.

It's best to put this into a little function you can call frequently. If the keyboard buffer is emptied before it's called, by some other code in your program, then when it's called, it will not have anything left to detect.

Some compilers support conio.h, (like Pelles C), but it's non-standard, and others don't. PDcurses is quite similar, and portable Windows or Linux (not sure about OSX though).

Windows has a couple ways to do it, this is one:
   HANDLE hConsole;
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   if (GetAsyncKeyState(VK_UP) & 0xf) {  // Up!
       //code to handle if the up arrow key (either one) is pressed
       //goes in here.
   }



This again, would need to be in a looping function, that was called frequently. In the game this is from, the player is running away from several muggers (a console game I wrote), and the key presses are fast and frequent. The function is called several times per second. Each key press you want to monitor has an if statement similar to the above. Probably not what you want, but from this, you know that Windows API has a way to monitor ANY keypress, and give you the hex value. (here: 0xf), and it does it asynchronously, so your program can continue to run like normal.

The other way to do it, would be to spawn a child (asynchronous) process (fork in Linux), and run the keypresses as a separate process - which is a bit more complex, but not massively so.

Although the conio.h or PDcurses way is simplest, on a Windows OS PC, I'd recommend using the Windows API, and just doing that bit more research on how to detect ANY keypress asynchronously, instead of specifying every key that you want to monitor and detect if pressed.

This post has been edited by Adak: 10 December 2013 - 12:43 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1