4 Replies - 1045 Views - Last Post: 22 January 2010 - 05:53 PM Rate Topic: -----

#1 alekk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-January 10

input key question

Posted 22 January 2010 - 03:22 PM

Hello everyone. First of all, I apologise for the bad topic title, but I couldn't find a better one. Here's the deal: I have implemented a function which checks if a key pressed was 'esc' or 'ctr+c'. In affirmative case, then it just exits the program. Else, it does nothing, it just waits for the user to press esc or ctrl+c. The function works, but if I press esc or ctrl+c before the function call, somehow the key stroke gets stored in memory, and my function executes, exiting the program. Here's the whole program:

#include <iostream>
#include <conio.h> // yes yes, bad for C++, it's a very small project though
#include <windows.h>

// The function that I'm talking about:
void WaitForEscKey()
{
	std::cout << "Press 'esc' or 'ctrl+c' to exit ..." << std::endl;
	bool finished = false;

	while (!finished)
	{
		int key = _getch();

		// 3 stands for CTRL-C and 27 for esc
		if (key == 3 || key == 27)
		{
			std::cout << "Exiting ";

			// generic
			for (int i = 1; i <= 3; i++)
			{
				Sleep(100);
				std::cout << ".";
			}

			finished = true;
		}
	}

	exit(1); // Exit directly
}

int main()
{
	// If I press 'esc' or 'ctrl+c' while the CPU 'sleeps', the function below executes ... 
	// The key stroke gets stored in memory. 
	Sleep(4000);
		
	// Here, I need to ignore every key that i pressed before the function call
	WaitForEscKey();

	return EXIT_SUCCESS;
}



How do I ignore every pressed key until my function call? I have tried std::cin.ignore() but it does not work ...

This post has been edited by alekk: 22 January 2010 - 03:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: input key question

#2 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,032
  • Joined: 14-September 07

Re: input key question

Posted 22 January 2010 - 03:37 PM

You can clear the buffer with cin.sync();

It would be better if you didn't use non standard functions for input.
Was This Post Helpful? 0
  • +
  • -

#3 alekk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-January 10

Re: input key question

Posted 22 January 2010 - 03:40 PM

View PostKYA, on 22 Jan, 2010 - 02:37 PM, said:

You can clear the buffer with cin.sync();

It would be better if you didn't use non standard functions for input.

I tried that too, but it appears that cin.sync() screws up the function functionality. If i use it, "Press 'esc' or 'ctrl+c' to exit" message does not even appear anymore, and i'm forced to close the program
About the non standard functions: well, i just didn't know any other way to get key input, rather than _getch()

This post has been edited by alekk: 22 January 2010 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,032
  • Joined: 14-September 07

Re: input key question

Posted 22 January 2010 - 04:46 PM

sync immediately before you get the input

Something interesting about _getch():

Quote

The _getch and _getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.


From here


Why not use cin >> ?
Was This Post Helpful? 0
  • +
  • -

#5 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 50
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: input key question

Posted 22 January 2010 - 05:53 PM

I am so tempted to write the code for you, but that would not teach you anything and DIC doesn't allow that.

Instead, I will guide you through it.

First of all, since you are using Windows.h, I assume this program is meant to only be compatible on windows. That being said, you can use window's ReadConsoleInput function. http://www.adrianxw..../Consoles5.html has a great tutorial on reading console input.

I will edit this post with how to do it once i finish debugging it.

This post has been edited by Splatocaster: 22 January 2010 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1