944,137 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7968
  • C++ RSS
Sep 27th, 2005
0

Getting Key Input

Expand Post »
I know how to use C++ to recieve input via
C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
vartotojas is offline Offline
29 posts
since Aug 2005
Sep 27th, 2005
0

Re: Getting Key Input

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 27th, 2005
0

Re: Getting Key Input

Quote 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

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Light Poster
vartotojas is offline Offline
29 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: counting then deleting multiples!!
Next Thread in C++ Forum Timeline: Create log file in Visual C++.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC