Getting Key Input

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 29
Reputation: vartotojas is an unknown quantity at this point 
Solved Threads: 0
vartotojas vartotojas is offline Offline
Light Poster

Getting Key Input

 
0
  #1
Sep 27th, 2005
I know how to use C++ to recieve input via
  1. cin >> x;

But now I want a way to constantly check for input without pressing enter.

To my knowledge with cin you must press enter to assign whatever they typed to a variable.

How could I make it so that when the left arrow key is pressed it adjusts a variable (No need to press enter.)

Thank you! (This is console mode)

As an example of what I want to do...When someone pushes down it would add 1 to row_number. If they push up I want it to subtract from row_number.

Same thing with row_column...

As a note all I want is a tutorial or the code to catch the key...Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Getting Key Input

 
0
  #2
Sep 27th, 2005
>To my knowledge with cin you must press enter to assign whatever they typed to a variable.
Correct. Input in standard C++ is line oriented, so to read raw input you need to use a system-dependent or compiler-dependent solution. This is offers three such solutions, but since you didn't tell us what compiler and operating system you're using, I can't help you any better than that.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 29
Reputation: vartotojas is an unknown quantity at this point 
Solved Threads: 0
vartotojas vartotojas is offline Offline
Light Poster

Re: Getting Key Input

 
0
  #3
Sep 27th, 2005
Originally Posted by Narue
>To my knowledge with cin you must press enter to assign whatever they typed to a variable.
Correct. Input in standard C++ is line oriented, so to read raw input you need to use a system-dependent or compiler-dependent solution. This is offers three such solutions, but since you didn't tell us what compiler and operating system you're using, I can't help you any better than that.
Thank You! I am currently using Dev-CPP.

Here is my code

  1. cout << "Please configure the keys!\nTap Left Now";
  2. int left = getch();
  3. cout << "Tap Right Now\n";
  4. int right = getch();
  5. cout << "Tap Up Now\n";
  6. int up = getch();
  7. cout << "Tap Down Now\n";
  8. int down = getch();

It works nicely except when I push Left it will assign right the same value. How would I stop that? With cin I could do cin.ignore...Is it something similar with this? Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC