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

c++: detecting keystrokes

Reply to Message Icon

Original Message
Name: jake1025
Date: November 26, 2003 at 19:27:08 Pacific
Subject: c++: detecting keystrokes
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



Report Offensive Message For Removal


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

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);


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

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


Report Offensive Follow Up For Removal

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

Jake:

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

MYXP


Report Offensive Follow Up For Removal


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

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


Report Offensive Follow Up For Removal

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

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.



Report Offensive Follow Up For Removal

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

IT WORKED!!

Thanks eveybody for helping me out!

-Jake


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: c++: detecting keystrokes

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes Today.
Discuss in The Lounge