Solved

how can i capture esc key press ?

Posted on 2000-03-08
16
368 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?
0
Comment
Question by:eddyhalim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 20 total points
ID: 2599668
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

int ReadKey(HANDLE hInput);
int HandleKey(KEY_EVENT_RECORD Key);

void main()
{
      HANDLE hInput;

      hInput = GetStdHandle(STD_INPUT_HANDLE);

      if ( ReadKey(hInput) == 27 )
            printf("Esc key pressed\n");
      else
            printf("Esc key not pressed\n");
}


int ReadKey(HANDLE hInput)
{
      DWORD Result;
      INPUT_RECORD Buffer;
      int KeyCode;

      flushall();
      do
      {
            ReadConsoleInput(hInput, &Buffer, 1, &Result);
            if ( Buffer.EventType == KEY_EVENT)
                  KeyCode = HandleKey(Buffer.Event.KeyEvent);
      } while ( Buffer.EventType != KEY_EVENT );

      return KeyCode;
}

int HandleKey(KEY_EVENT_RECORD Key)
{
      return Key.wVirtualKeyCode;
}

Hope it helps
hongjun
0
 

Expert Comment

by:wytze
ID: 2599699
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
}
0
 
LVL 33

Expert Comment

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

hongjun
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:wytze
ID: 2600058
Ok, but is he using windows?
0
 
LVL 33

Expert Comment

by:hongjun
ID: 2600379
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
0
 

Author Comment

by:eddyhalim
ID: 2603142
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.?
0
 
LVL 33

Expert Comment

by:hongjun
ID: 2603178
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
0
 

Author Comment

by:eddyhalim
ID: 2604030
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.
0
 

Author Comment

by:eddyhalim
ID: 2604035
hongjun, i am foreign not singaporean.
by the way what are u studying now?.
0
 

Expert Comment

by:wytze
ID: 2604133
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.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 2606711
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
0
 

Author Comment

by:eddyhalim
ID: 2608916
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)
0
 
LVL 33

Expert Comment

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

hongjun
0
 

Author Comment

by:eddyhalim
ID: 2618581
thanks anyway for your info.
0
 

Author Comment

by:eddyhalim
ID: 2630301
thx to wytze
0
 

Author Comment

by:eddyhalim
ID: 2630302
thx for your help wytze
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question