For example, I always want alt+f4 to close the game, ctrl to silence speech, and pg up and pg down to adjust music volume. So, I always have a function like this:
void system_keys() {
if(key_pressed(KEY_F4) and (key_down(KEY_LMENU) or (key_down(KEY_RMENU))) exit();
else if(key_down(KEY_LCONTROL) or key_down(KEY_RCONTROL)) voice.stop();
if(key_pressed(key_prior)) change_bgm_volume(5);
else if(key_pressed(KEY_NEXT)) change_bgm_volume(-5);
}
If I have a while loop, the first line is always system_keys(); ex:
while (contmenu) {
system_keys();
bool speak=true;
if (key_pressed(KEY_DOWN)) menu_x++;
else if(key_pressed(KEY_UP)) menu_x--;
else if (key_pressed(KEY_ENTER)) {
contmenu=false;
select_menu_item(menu_x);
speak=false;
}
else speak=false;
if (menu_x<0) menu_x=0
else if (menu_x>=menu_items.length()) menu_x=menu_items.length()-1;
if (speak) voice.speak_interrupt(menu_items[menu_x]);
wait(10);
}
Or, if I'm using a dynamic_menu:
int callback (dynamic_menu@ menu, string data) {
system_keys();
// whatever you need the callback to do.
return 0;
}
And you'd use menu.set_callback(callback, data); to set the callback, so that system_keys still works within dynamic_menus.
And if you were using a more event-based system, I would probably put system_keys at the top of your key_pressed function.
看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
George... Don't do that.