Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Krypto night

escape characters

This topic is 5317 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

How do i create a character in c++ where if the user presses it at any time during the program it exits. Eg/ a program runs until the user preses q or even ctrl + q. Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
I might be wrong on this

but at the top in your defines add,


#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)



Then in your main game loop you can add something such as,


// In your case I think you'd replace VK_ESCAPE with
// 51 which is the hexadecimal value for the Q key

if (KEY_DOWN(VK_ESCAPE))
{
PostMessage(main_window_handle, WM_DESTROY,0,0);
}



As for CTRL-Q? I have no idea

[edited by - _vizual_ on January 24, 2004 3:48:41 PM]

Share this post


Link to post
Share on other sites
Although it works in a mfc application in wont work in a win console application.

Does any one know how to solve the problem in a console application?

Share this post


Link to post
Share on other sites
the only way i can think of is each time you ask for input, allow a "quit" input. For example, if you''re asking for a choice of 0-9, you accept all input 0-9 as well as q, where if the input''s q you quit.

If, however, you''re for example calculating a billion numbers and you want to allow the user to quit inbetween you could setup intervals (like every 10 million calculatoins) at which point you ask the user whether or not he wishes to quit. Also, it''d be a good idea to insert a timer so that after x seconds it goes on (if no input was given).

As for an example, here''s my (rather messy) input loop from my console game:
while(!chosen){
i=getint();
if(i>=0&&i<=6){ /* needs to be changed if menu is expanded! */
choice=i;
chosen=true;
}else if(i+48==''x''||i+48==''X''||i+48==''q''||i+48==''Q''){
do{
std::cout<<"Are you sure you wish to quit? (Y/N)"<<std::endl;
quit=getch();
}while(quit!=''n''&&quit!=''N''&&quit!=''y''&&quit!=''Y'');
if(quit==''y''||quit==''Y''){
quitting=true;
chosen=true;
}
}
}
chosen=false;
input=''\0'';

in case you''re wondering, getint() is just a getch() + 48.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!