wxWidgets not recognizing ctrl key in key events Topic is solved

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
vielmaj
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Feb 08, 2018 5:56 am

wxWidgets not recognizing ctrl key in key events

Post by vielmaj »

Hello,

I am using a richtextctrl that is partly read-only. So in my code, I have this

Code: Select all

//key events
void MainDialog::PressKey(wxKeyEvent &event)
{
   if( event.GetKeyCode() == WXK_BACK &&
             GetCaretPosition() <= lastReadyOnlyCursorPosition)
    {
        if( event.ControlDown() )
        {
            cout << "Catch control backspace!" << endl;
        }
    }
    else
    {
        event.Skip();
    }
}
That at a certain position I disable the backspace button. It works unless I type ctrl-backspace. Then it overrides my key event.
I've tried catching it with using event.ControlDown(). That doesn't work. I've even tried event.HasAnyModifiers(), event.RawControlDown() , event.MetaDown() to even notice the control is being down. HasAnyModifiers does notice when I hold down SHIFT and ALT, but not CTRL.

Thanks for any help.

Jason
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWidgets not recognizing ctrl key in key events

Post by doublemax »

When i run the "keyboard" sample that comes with wxWidgets, it shows that the CHAR event for "Ctrl-Backspace" is WXK_DELETE, not WXK_BACK. I don't know if this is a bug or the normal behavior under Windows, but that's probably why your code doesn't work as intended.
Use the source, Luke!
vielmaj
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Feb 08, 2018 5:56 am

Re: wxWidgets not recognizing ctrl key in key events

Post by vielmaj »

Thanks for the reply. I did the keyboard test and it just says I am just holding down CTRL and pressing BACK.

I did a web search and found ctrl-backspace is delete previous word. I cannot find the wxID key to disable it.

How would I disable it?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWidgets not recognizing ctrl key in key events

Post by doublemax »

I did the keyboard test and it just says I am just holding down CTRL and pressing BACK.
Which platform are you using?

Which keyevent are you catching (CHAR, KEY_UP or KEY_DOWN)?
How would I disable it?
First you have to check if the event handler is actually called when pressing. If yes, just check which ID it contains.
Use the source, Luke!
vielmaj
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Feb 08, 2018 5:56 am

Re: wxWidgets not recognizing ctrl key in key events

Post by vielmaj »

Thanks for your help. I figured out a solution!

I added the following member function in the header file

Code: Select all

void KeyDown(wxKeyEvent &event);
I added the following to the Event Table

Code: Select all

EVT_KEY_DOWN(MainDialog::KeyDown)
The following member function removes ctrl+backspace and lets all other keydown pass.

Code: Select all

void MainDialog::KeyDown(wxKeyEvent &event)
{
    if( event.GetKeyCode() == WXK_BACK &&
        event.GetModifiers() == wxACCEL_CTRL  )
    {
    }
    else
    {
        event.Skip();
    }
}
Post Reply