Archived

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

Krypto night

escape characters

Recommended Posts

Krypto night    122
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
_vizual_    122
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
kVandaele    138
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