I have run into a wxGetKeyState()
issue with Wayland. Let me explain: In some of my apps, I add a test for the Shift key being pressed in the ctor of my app’s top wxFrame window. If the Shift key is down during launch, I run diagnostic code relevant to my app. This has always worked just fine until I switched to Ubuntu 22.04 with the Wayland display server. If I run my app in Ubuntu 22.04 with the X.org display sever, everything runs as expected. By the way, I’m using wxWidgets 3.2.0.
To test this possible bug just add these few lines of code to the end of the top wxFrame ctor.
MyFrame::MyFrame()
{
...
if (wxGetKeyState(WXK_SHIFT))
{
wxMessageBox("Hello there");
}
}
Does anyone have run into this issue? Is there a known work-around?
Regards, Bob
EDIT: When I run the minimal app (shown below) I see these results for X and Wayland.
Output when launching the app while holding the shift key down in X.
18:40:25: Debug: from CTOR: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
18:40:25: Debug: from idle: 1
Under X, wxGetKeyState()
behaves as expected. It goes thru 16 idle cycles before stopping while showing the correct value all along.
Now, this is the output when launching the app while holding the shift key down in Waylan.
18:32:43: Debug: from CTOR: 0
18:32:43: Debug: from idle: 0
18:32:43: Debug: from idle: 0
18:32:43: Debug: from idle: 0
18:32:43: Debug: from idle: 1
Under Wayland, the test at the ctor fails and it takes 3 idle cycles before reporting the correct value. I hope this test helps to identify and solve this issue.
Minimal test program:
#include <wx/wx.h>
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame();
void on_idle(wxIdleEvent& event);
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame* wnd = new MyFrame();
wnd->Show();
return true;
}
MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "minimal")
{
Bind(wxEVT_IDLE, &MyFrame::on_idle, this);
wxLogDebug("from CTOR: %d", wxGetKeyState(WXK_SHIFT));
}
void MyFrame::on_idle(wxIdleEvent& event)
{
wxLogDebug("from idle: %d", wxGetKeyState(WXK_SHIFT));
}