Computing.Net > Forums > Programming > c++: detecting keystrokes

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

c++: detecting keystrokes

Reply to Message Icon

Name: jake1025
Date: November 26, 2003 at 19:27:08 Pacific
OS: ME
CPU/Ram: 800 PIII, 128
Comment:

DOES ANY BODY KNOW HOW TO DETECT A KEYSTROKE IN C++ AND DETERMINE WHAT KEY WAS PRESSED??? OR BASICLY A "cin >>" WHERE YOU DON'T HAVE TO PRESS <enter> AT THE END OF THE INPUT.
Im trying to make an interactive menu where you can press the up or down keys to select an item.
Example:

cout<<"New Game<- \nOptions";
onKeyPress("enter"){
startGame();//open this function
}
onKeyPress("down"){
system("CLS"); //clear screen
cout<<"New Game \nOptions<-";
onKeyPress("enter"){
setOptions();//open this function
}
}

so users don't have to press enter to select an option:

int a;
cout<<"New Game = 1\nOptions = 2\n";
cin >>a;
swich (a){
case 1:
startGame();
case 2:
setOptions();
default:
cout<<"\nERROR!";
}

can somebody post an example or some helpfull links?

Thanks,
Jake




Sponsored Link
Ads by Google

Response Number 1
Name: Ronin1
Date: November 26, 2003 at 19:53:28 Pacific
Reply:

cin is designed for buffered input. If your compiler has it, look for the non-standard function getch... usually included with conio.h

unsigned key;

do{

key = getch();
switch(key){...}

}while(some condition);


0

Response Number 2
Name: myxp
Date: November 27, 2003 at 04:04:56 Pacific
Reply:

Jake:

This code will capture and show any
combination of keys in ascii or special
keys (like CTRL+A and so on) without
pressing <enter>.

int main(int argc, char **argv)
{

/*
press "q" (lower case letter only)
to quit */
*/

unsigned int key;
do
{
key = getch();
printf("key = %d\n", key);
}
while(key != 'q');
return 0;

}


MYXP


0

Response Number 3
Name: jake1025
Date: November 27, 2003 at 13:53:54 Pacific
Reply:

Thanks for the examples, but they don't work the way I expected them to. You still have to hit enter after you press a key. It may be my compiler. I use Bloodshed Dev-C++ 4.0. If you don't have to hit enter after you press a key, tell me what compiler you use.

Thanks,
Jake


0

Response Number 4
Name: Ronin1
Date: November 27, 2003 at 15:02:51 Pacific
Reply:

In myxp's example, you can add an additional call to getch(e). If the outer call is 0, then you've got extended keys, so trap the inner call for the special keys. ie: arrows, function keys, etc.

Try getche() in Dev C. However it will output the character, so call putch('\x8'); after it if you wish not to have an echo.

HTH


0

Response Number 5
Name: myxp
Date: November 27, 2003 at 17:58:19 Pacific
Reply:

Jake:

Can you compile assembly source code with the compiler you're using?

MYXP


0

Related Posts

See More



Response Number 6
Name: jake1025
Date: November 27, 2003 at 18:23:44 Pacific
Reply:

Ronin1:
Thanks for the reply, but I am kinda new to C++. Can you give me an example of how this might work? Where do you place "putch('\x8');"? And not to sound rude, but what do you mean by "outer call is 0"? Is this some var I have to set - or the number I put petween the parenthesis? I'm kinda confused, so bear with me.

Thanks in advance,
Jake


0

Response Number 7
Name: Ronin1
Date: November 27, 2003 at 20:30:28 Pacific
Reply:

Don't worry about rudeness. If you cross the line, then I won't reply. :P

unsigned key1, key2;

do
{
key1 = getche(); // outer test
putch('\x8'); // put backspace char

if(key1 == 0) // means that a special key was pressed
{
key2 = getche(); // inner
putch('\x8');

switch(key2)
{
case 72: /* up arrow */ break;
...
}// end switch
}//end if
}while(key1 != 27); // esc key pressed?

You need to be careful with getch in OSes like XP, NT, or 2K since they emulate console output. They can go nuts.



0

Response Number 8
Name: jake1025
Date: November 29, 2003 at 09:07:16 Pacific
Reply:

IT WORKED!!

Thanks eveybody for helping me out!

-Jake


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: c++: detecting keystrokes

How do I detect keystrokes? www.computing.net/answers/programming/how-do-i-detect-keystrokes/15587.html

Detect Mouse in C++ www.computing.net/answers/programming/detect-mouse-in-c/13117.html

JAVA collision detection etc www.computing.net/answers/programming/java-collision-detection-etc/14141.html