C++ checking input for F keys?

Hi, I hope someone here can lead me in the right direction.
I use MS Visual Studio 2008 and Eclipse for editing and compiling my code.

I go a problem with following code:

1
2
3
4
5
6
7
8
9
10
while(getGateType = _getch())											//validating the users input
    	{																		
		if(getGateType > '0' && getGateType < '7')								
			break;
			coutAlignError(0,60,22,"You did not select one of the available gates!"); //if the user input isn't valid we notify with an onscreen message
		}
    	
    	coutAlign(0,60,22,"");													//if there is an error message it gets removed

		GateType = (int)getGateType - '0';										//converting char to int with the same numeric value 


The problem as you probably already can see from the title, is that my code doesn't stop the F1 - F12 keys, with the result that I get an unvalid input send down to the rest of my code.
Thing that happens is, it checks the first byte, but not the complete key input at once.
So if that first byte is between 1 and 6 it goes through together with the remaining 2-3 bytes..
I tried different approaches, like declaring a string, checking its length etc. but so far nothing seemed to work out for me.
Is there a way to limit the input stream in such way that my program only accepts the very first byte in every keystroke and cuts of the rest?
If some might need to look over the complete code to have a better overview, it can be found on OpenSVN; https://opensvn.csie.org/traccgi/cpp_projects/browser/Logical_Gates/src



The F keys (and the arrows, etc) emit two bytes. The first is either 0 or 0xE0. So you cannot control your loop testing the return of _getch() since an F key that returns zero as its first byte will kill it. Here's some test code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <conio.h>

const int ESCAPE = 27;

int main(){
    char c;
    while( (c = _getch()) != ESCAPE ){
        if( c == 0 || c == '\xE0' )
            // Be sure to eat the second byte.
            std::cout << "F key: " << _getch() << "\n";
        else
            std::cout << (int)c << '\n';
    }
}

Last edited on
Thanks for giving me the clue upon how to read it out.
I restricted it to 1 (array had no effect), so now it stays in the loop until 1 or a valid key is pressed.

1
2
3
4
5
6
7
8
const int key1 = 49;

while((getGateType = _getch()) != key1)
{
	if(getGateType > '0' && getGateType < '7')
	break;
	coutAlignError(0,60,22,"You did not select one of the available gates!");
}
Last edited on
Topic archived. No new replies allowed.