We help IT Professionals succeed at work.

how can i capture esc key press ?

eddyhalim
eddyhalim asked
on
419 Views
Last Modified: 2012-06-27
i tried to use getche() but there is a problem with it.


cout<<"please enter the key :";
cout<<endl;
choice = getche();


if we are not using endl the cout will not appear until we press akey.
so is there another way to write it?
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
cout <<"Press a key"<<flush;
                        choise=getch();
if (choise == 0x00){
      // function key
      choise = getch();
}else if (choise == 27) {
      // escape;
      cout << "ESC";
}else {
      cout << choise;
      // do some thing with choise
}

Commented:
One problem using getch() is that it cannot accept key like CAPS lock. ReadConsoleInput function can! But remember to include <windows.h>

hongjun

Commented:
Ok, but is he using windows?

Commented:
I am just trying to help. This is definitely a better one since it can read in any key even if the key input is a system key.

hongjun

Author

Commented:
thanks for all your help,I'll try it first.

Hong jun,
you are singaporean, i have some question to ask due to University  for Computer Science in your country. Which one is the best university in Computerscience.?

Commented:
Are you a Singaporean or a foreign? Basically there are currently 2 Universities in Singapore - National University of Singapore(NUS) and Nanyang Technological University(NTU). I feel that NUS is better in your field you want.

By the way, ReadConsoleInput function can be used even if you are not creating a Win32 window.

hongjun

Author

Commented:
wytze thanks a lot for your help but using flush it's not work. it's also almost same as endline.
, i finally found it the way change the cout by using the printf ot cputs.

Author

Commented:
hongjun, i am foreign not singaporean.
by the way what are u studying now?.

Commented:
if you want no buffering in your output, you could try the follwing:

cout.setf(ios::unitbuf);

cout << "Hello" << " this" << " an";

the output is now flushed per unit. In this case three times.

The advantage above printf en cputs is that u remain in pure C++, some regard this as a disadvantage but that is matter of taate.

Commented:
I am studying in Singapore Polytechnic studying Computer Information Systems. I am studying programming like C++, VB 6.0, Windows Programming, Networking and security to list a few.

University is better than a Polytechnic and so if you can enrol yourself in a University then go for it. You will definitely learn more than me.

hongjun

Author

Commented:
i'm now studying in collage in malaysia. taking a degree in Computer science too.I'm learning programming such as VC++6(new),VB6,delphi, assembly.and also i still learning some graphic design such as 3dmax, and photoshop(i learn by myself)

Commented:
Me too! I also learn Photoshop by myself. My course do not teach Photoshop and so have to learn by myself.

hongjun

Author

Commented:
thanks anyway for your info.

Author

Commented:
thx to wytze

Author

Commented:
thx for your help wytze
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.