/** * \brief This function is called by the game when a low-level event occurs. * \param event An input event. */ void GameCommands::notify_input(const InputEvent& event) { // If no game command is being customized, we look for a binding // for this input event and we ignore the event if no binding is found. // If a command is being customized, we consider instead this event as // the new binding for this game command. if (event.is_keyboard_key_pressed()) { keyboard_key_pressed(event.get_keyboard_key()); } else if (event.is_keyboard_key_released()) { keyboard_key_released(event.get_keyboard_key()); } else if (event.is_joypad_button_pressed()) { joypad_button_pressed(event.get_joypad_button()); } else if (event.is_joypad_button_released()) { joypad_button_released(event.get_joypad_button()); } else if (event.is_joypad_axis_moved()) { joypad_axis_moved(event.get_joypad_axis(), event.get_joypad_axis_state()); } else if (event.is_joypad_hat_moved()) { joypad_hat_moved(event.get_joypad_hat(), event.get_joypad_hat_direction()); } }
/** * @brief Notifies the object on top of the stack * that a keyboard key was just pressed * (including if it is a directional key or a character). * @param event The corresponding input event. * @return \c true if the event was handled and should stop being propagated. */ bool LuaContext::on_key_pressed(InputEvent& event) { bool handled = false; if (find_method("on_key_pressed")) { const std::string& key_name = InputEvent::get_keyboard_key_name(event.get_keyboard_key()); if (!key_name.empty()) { // This key exists in the KQ API. push_string(l, key_name); lua_newtable(l); /* if (event.is_with_shift()) { lua_pushboolean(l, 1); lua_setfield(l, -2, "shift"); } if (event.is_with_control()) { lua_pushboolean(l, 1); lua_setfield(l, -2, "control"); } if (event.is_with_alt()) { lua_pushboolean(l, 1); lua_setfield(l, -2, "alt"); } */ bool success = call_function(3, 1, "on_key_pressed"); if (!success) { // Something was wrong in the script: don't propagate the input to other objects. handled = true; } else { handled = lua_toboolean(l, -1); lua_pop(l, 1); } } else { // The method exists but the key is unknown. lua_pop(l, 2); // Pop the object and the method. } } return handled; }
/** * \brief This function is called when there is an input event. * * It handles the events common to all screens: * closing the window, pressing F5 or a debug key. * The notify_input() method of the current screen * is then called. */ void MainLoop::notify_input(const InputEvent& event) { if (event.is_window_closing()) { exiting = true; } else if (event.is_keyboard_key_pressed()) { // A key was pressed. #if defined(PANDORA) // TODO make a clean flag if (event.get_keyboard_key() == InputEvent::KEY_ESCAPE) { exiting = true; } #endif } // Send the event to Lua and to the current screen. bool handled = lua_context->notify_input(event); if (!handled && game != NULL) { game->notify_input(event); } }
/** * @brief This function is called when there is an input event. * * It handles the events common to all screens: * closing the window, pressing F5 or a debug key. * The notify_event() method of the current screen * is then called. */ void Solarus::notify_event(InputEvent& event) { // handle the common events InputEvent::KeyboardKey key = event.get_keyboard_key(); if (event.is_window_closing()) { exiting = true; } else if (event.is_keyboard_key_pressed()) { // a key is pressed if (key == InputEvent::KEY_F5) { // F5: change the video mode VideoManager::get_instance()->switch_video_mode(); } else if (key == InputEvent::KEY_RETURN && (event.is_alt_down() || event.is_control_down())) { // Alt + Return or Ctrl + Return: switch fullscreen VideoManager::get_instance()->switch_fullscreen(); } else if (key == InputEvent::KEY_F4 && event.is_alt_down()) { // Alt + F4: quit the program exiting = true; } #if defined(PANDORA) else if (key == InputEvent::KEY_ESCAPE) { exiting = true; } #endif else { debug_keys->key_pressed(key); } } else if (event.is_keyboard_key_released()) { // a key is released debug_keys->key_released(key); } // send the event to the current screen current_screen->notify_event(event); }
void GameControls::notify_input(InputEvent &event) { if(event.is_keyboard_key_pressed()) key_pressed(event.get_keyboard_key()); else if(event.is_keyboard_key_released()) key_released(event.get_keyboard_key()); }