Assigning keyboard shortcuts to controls.
Re: Assigning keyboard shortcuts to controls.
Yes, the events only inform you about what happened, i.e. if a checkbox was toggled, you get the event telling you about it. But it doesn't work in the other direction, just sending the event won't change the state of the checkbox.
Use the source, Luke!
Re: Assigning keyboard shortcuts to controls.
I made the App class friend in MainFrame, in MainFrame.hpp file I have this,doublemax wrote: ↑Wed May 12, 2021 6:13 pm No. You can't just cast a wxKeyEvent to a wxCommandEvent.
E.g. to emulate a button click event:Code: Select all
wxCommandEvent evt(wxEVT_BUTTON); // this two are not always needed, depends on your event binding and // whether the event handlers use these values evt.SetId( /* control id */ ); evt.SetEventObject( /* control pointer */ ); m_Frame->AddPendingEvent(evt);
Code: Select all
friend class App;
Code: Select all
int App::FilterEvent(wxEvent& event)
{
if(event.GetEventType() == wxEVT_KEY_DOWN /* || event.GetEventType() == wxEVT_KEY_UP */)
{
wxWindow* focusedWindow = wxWindow::FindFocus();
if(focusedWindow != NULL)
{
const wxString& className = focusedWindow->GetClassInfo()->GetClassName();
wxLogDebug("Focused window: %s", className);
// if SearchCtrl has focus, let all keys through
if(className == wxT("wxSearchCtrl"))
{
wxLogDebug("SearchCtrl focused skipping key filtering");
return wxEventFilter::Event_Skip;
}
else if(className == wxT("wxTreeCtrl"))
{
wxLogDebug("TreeCtrl focused skipping key filtering");
return wxEventFilter::Event_Skip;
}
}
wxKeyEvent* keyEvent = wxDynamicCast(&event, wxKeyEvent);
wxCommandEvent playEvent(wxEVT_BUTTON);
wxCommandEvent muteEvent(wxEVT_TOGGLEBUTTON);
wxCommandEvent loopEvent(wxEVT_TOGGLEBUTTON);
wxCommandEvent stopEvent(wxEVT_BUTTON);
wxCommandEvent autoplayEvent(wxEVT_CHECKBOX);
wxCommandEvent settingsEvent(wxEVT_BUTTON);
// Setting ID and EventObject to their handlers
playEvent.SetId(BC_Play);
playEvent.SetEventObject(m_Frame->m_PlayButton);
muteEvent.SetId(BC_Mute);
muteEvent.SetEventObject(m_Frame->m_MuteButton);
loopEvent.SetId(BC_Loop);
loopEvent.SetEventObject(m_Frame->m_LoopButton);
stopEvent.SetId(BC_Stop);
stopEvent.SetEventObject(m_Frame->m_StopButton);
autoplayEvent.SetId(BC_Autoplay);
autoplayEvent.SetEventObject(m_Frame->m_AutoPlayCheck);
playEvent.SetId(BC_Settings);
playEvent.SetEventObject(m_Frame->m_SettingsButton);
// Check for key presses
if(keyEvent != NULL)
{
switch (keyEvent->GetKeyCode())
{
case WXK_SPACE:
wxLogDebug("Space pressed");
return wxEventFilter::Event_Processed;
break;
case 'A':
if (m_Frame->m_AutoPlayCheck->GetValue())
m_Frame->m_AutoPlayCheck->SetValue(false);
else
m_Frame->m_AutoPlayCheck->SetValue(true);
m_Frame->OnCheckAutoplay(autoplayEvent);
wxLogDebug("A pressed");
return wxEventFilter::Event_Processed;
break;
case 'S':
m_Frame->OnClickStop(stopEvent);
wxLogDebug("S pressed");
return wxEventFilter::Event_Processed;
break;
case 'P':
m_Frame->OnClickPlay(playEvent);
wxLogDebug("P pressed");
return wxEventFilter::Event_Processed;
break;
case 'O':
m_Frame->OnClickSettings(settingsEvent);
wxLogDebug("O pressed");
return wxEventFilter::Event_Processed;
break;
case 'L':
if (m_Frame->m_LoopButton->GetValue())
m_Frame->m_LoopButton->SetValue(false);
else
m_Frame->m_LoopButton->SetValue(true);
m_Frame->OnClickLoop(loopEvent);
wxLogDebug("L pressed");
return wxEventFilter::Event_Processed;
break;
case 'M':
if (m_Frame->m_MuteButton->GetValue())
m_Frame->m_MuteButton->SetValue(false);
else
m_Frame->m_MuteButton->SetValue(true);
m_Frame->OnClickMute(muteEvent);
wxLogDebug("M pressed");
return wxEventFilter::Event_Processed;
break;
}
}
}
return -1;
}
Re: Assigning keyboard shortcuts to controls.
Code: Select all
wxCommandEvent playEvent(wxEVT_BUTTON);
wxCommandEvent muteEvent(wxEVT_TOGGLEBUTTON);
wxCommandEvent loopEvent(wxEVT_TOGGLEBUTTON);
wxCommandEvent stopEvent(wxEVT_BUTTON);
wxCommandEvent autoplayEvent(wxEVT_CHECKBOX);
wxCommandEvent settingsEvent(wxEVT_BUTTON);
Use the source, Luke!
Re: Assigning keyboard shortcuts to controls.
So it is safe to assume this will work okay, and will not cause any problems?doublemax wrote: ↑Thu May 13, 2021 6:27 amI wouldn't create all wxCommandEvents at the same time, i'd move them into their respective switch case, but in principle this looks ok.Code: Select all
wxCommandEvent playEvent(wxEVT_BUTTON); wxCommandEvent muteEvent(wxEVT_TOGGLEBUTTON); wxCommandEvent loopEvent(wxEVT_TOGGLEBUTTON); wxCommandEvent stopEvent(wxEVT_BUTTON); wxCommandEvent autoplayEvent(wxEVT_CHECKBOX); wxCommandEvent settingsEvent(wxEVT_BUTTON);
Re: Assigning keyboard shortcuts to controls.
I can't make any promises, this is still very hackish after all, but i think it should work.
Use the source, Luke!