• Advertisement
Sign in to follow this  

How to check if Escape is pressed?

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for this highly stupid question: How can I check if Escape, Shift, Ctrl, F1, F2, F3, etc are pressed with standard C functions? I just cannot remember how to do it and I don't have a book around here. [Edited by - NamelessTwo on March 30, 2006 1:52:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
There is no standard c function to do that because it is OS-specific. What OS are you using?

Share this post


Link to post
Share on other sites
I think in Pascal it was possible to detect such keys somehow so I don't think it is OS specific.

I'm using WinXP but the program should run under DOS.

Share this post


Link to post
Share on other sites
Maybe something like this is what you want.

#define ESC 27
char c=getch();
if (c==ESC)
{
...
}

Share this post


Link to post
Share on other sites
Quote:
Original post by NamelessTwo
I think in Pascal it was possible to detect such keys somehow so I don't think it is OS specific.

I'm using WinXP but the program should run under DOS.


It is likely OS-specific in Pascal as well.

Share this post


Link to post
Share on other sites
This should do the trick:

char get_key (bool& special)
// Wait for a key to be pressed, then return its ASCII code.
// For a two-code special key sequence, set special to true
// and return the special key code (e.g. numeric keypad)
{
char key;
while (!kbhit()); // wait
key = getch();
if (key == 0) // check for special key
{
special = true;
key = getch();
}
else
special = false;
return key;
}

Share this post


Link to post
Share on other sites
Did you just answer your own question?

As for if you want to use this in a graphical game, not just a console program, it will be OS-specific. However, you could use a library like SDL that is not platform-specific(runs on Linux, Mac, and Windows).

Share this post


Link to post
Share on other sites
Quote:
Original post by NamelessTwo
This should do the trick:

char get_key (bool& special)
// Wait for a key to be pressed, then return its ASCII code.
// For a two-code special key sequence, set special to true
// and return the special key code (e.g. numeric keypad)
{
char key;
while (!kbhit()); // wait
key = getch();
if (key == 0) // check for special key
{
special = true;
key = getch();
}
else
special = false;
return key;
}


That method won't catch the Ctrl and Shift keys. To do that, you need OS-specific API functions - GetAsyncKeyState() in Win32 for example.


Share this post


Link to post
Share on other sites
Or, a cross platform input library. SDL works, but would be a bloated option if all you're doing is input.

Share this post


Link to post
Share on other sites
The simplest way to check if escape is pressed in windows is:

if(GetAsyncKeyState(VK_ESCAPE)) {
// Code
}

Share this post


Link to post
Share on other sites
In borland turbo pascal there was an easy way to use port 60h and 61h to read keyboard signals. The advantage was, it could be done asynchronously. This, while commonly supported back in the day, was platform specific.

Your loop blocks until a key is pressed, which may or may not be desirable. And just as a portability warning, from kbhit documentation:

Prototype: int kbhit(void);
Header File: conio.h
Explanation: This function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland's family of compilers. It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed.

kbhit is highly platform specific, and is generally not support under *nix, or even any non-windows compilers. There are many workarounds, but it's not portable by any means. Reading from description, and mention of Borland, I'd say the implementation uses the Pascal approach, but this is just a guess.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement