Skip to content

Commit d9e8b34

Browse files
committed
Allow background use for gamepad input
1 parent f622d87 commit d9e8b34

1 file changed

Lines changed: 20 additions & 29 deletions

File tree

SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_CommandRow.cpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,23 @@ CommandRow::CommandRow(
177177
}
178178
#endif
179179

180-
m_session.add_listener(*this);
181-
m_controller.add_listener(*this);
182-
// global_input_add_listener(*this);
183-
}
180+
m_session.add_listener(*this);
181+
m_controller.add_listener(*this);
182+
global_input_add_listener(*this);
183+
}
184184

185185

186186
bool CommandRow::allow_controller_input() const{
187187
return m_allow_commands_while_running || m_last_known_state == ProgramState::STOPPED;
188188
}
189189
void CommandRow::run_controller_input(ControllerInputState& state){
190-
if (!m_last_known_focus){
191-
m_controller.logger().log("Input Command Suppressed: Not in focus.", COLOR_RED);
192-
return;
190+
const bool is_keyboard = state.type() == ControllerInputType::HID_Keyboard;
191+
if (is_keyboard && !m_last_known_focus){
192+
// Allow neutral keyboard states through so held keys are released on focus changes.
193+
if (!state.is_neutral()){
194+
m_controller.logger().log("Keyboard Input Suppressed: Not in focus.", COLOR_RED);
195+
return;
196+
}
193197
}
194198
AbstractController* controller = m_controller.controller();
195199
if (controller == nullptr){
@@ -202,28 +206,15 @@ void CommandRow::run_controller_input(ControllerInputState& state){
202206
}
203207
controller->run_controller_input(state);
204208
}
205-
void CommandRow::set_focus(bool focused){
206-
if (focused){
207-
global_input_add_listener(*this);
208-
if (allow_controller_input()){
209-
}
210-
}else{
211-
global_input_clear_state();
212-
global_input_remove_listener(*this);
213-
214-
AbstractController* controller = m_controller.controller();
215-
if (controller != nullptr && allow_controller_input()){
216-
try{
217-
controller->cancel_all_commands();
218-
}catch (InvalidConnectionStateException&){
219-
}catch (OperationCancelledException&){
220-
}
221-
}
222-
}
223-
if (m_last_known_focus == focused){
224-
return;
225-
}
226-
m_last_known_focus = focused;
209+
void CommandRow::set_focus(bool focused){
210+
if (!focused){
211+
// Keep gamepad input alive in the background. Only clear keyboard state.
212+
global_keyboard_tracker().clear_state();
213+
}
214+
if (m_last_known_focus == focused){
215+
return;
216+
}
217+
m_last_known_focus = focused;
227218
update_ui();
228219
}
229220

0 commit comments

Comments
 (0)