Jump to content
  • Advertisement
Sign in to follow this  
NamelessTwo

How to check if Escape is pressed?

This topic is 4641 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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!