How to check if a key is pressed?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
apoorv569
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Tue Oct 20, 2020 3:35 pm

How to check if a key is pressed?

Post by apoorv569 »

I have a wxPanel on which I have a wxEVT_MOTION, wxEVT_LEFT_DOWN and wxEVT_LEFT_UP event set up. But I want to do something else if say Ctrl + LMB is pressed instead of just LMB. So I tried adding this to the wxEVT_LEFT_DOWN as a check before I do something with it,

Code: Select all

    wxKeyboardState kbd_state;
    if (kbd_state.ControlDown())
        wxLogDebug("Control pressed");
But this is not doing anything, doesn't print the statement. I also tried catching wxEVT_KEY_DOWN,

Code: Select all

void WaveformViewer::OnPressCtrl(wxKeyEvent& event)
{
    if (event.ControlDown() && this->HasFocus())
        wxLogDebug("Panel has focus and control pressed.");
}
But this also does nothing. What am I doing wrong?
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to check if a key is pressed?

Post by doublemax »

wxMouseEvent derives from wxKeyboardState, so you can just use event.ControlDown() in a mouse event handler.
Use the source, Luke!
Post Reply