escape characters

Started by
3 comments, last by Krypto night 20 years, 3 months ago
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.
--------------------------------MSDN
Begginers
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 keyif (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]
cheers,vizuäl
Even though i dont understand the code, it works.

Thanks

[edited by - krypto night on January 24, 2004 4:47:43 PM]
--------------------------------MSDN
Begginers
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?
--------------------------------MSDN
Begginers
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.

This topic is closed to new replies.

Advertisement