c++wxwidgetswxgrid

How to catch KeyEvents when wxGrid Editor is Shown


When typing in a cell in wxGrid, the arrow keys function to go back and forward between the characters. However, depending on caret position I would like to move the cursor to the next cell when an arrow key is pressed. Currently, wxGrid is configured to capture Enter key, which makes the cursor to move downwards (to the cell below). My question is how can I capture KeyEvent when editor is still shown.

My approach:

void Grid::OnGridCmdEditorShown( wxGridEvent& event )
{
    m_IsEditorShown=true;
    //Connect(wxEVT_KEY_DOWN,wxKeyEventHandler(Grid::OnKeyDown),NULL, this); //This approach did not help either
    event.Skip();
}

void Grid::OnKeyDown(wxKeyEvent& event)
{
    if(m_IsEditorShown) wxMessageBox("You are keying");

    event.Skip();
}

When the editor is shown and say I type abc to the current cell, the MessageBox only appears when I press enter. How can catch the KeyEvents when the editor is still shown, for example, the user types a to the current cell and the MessageBox is shown.


Solution

  • One way that worked for me was to connect a handler to each grid editor after it had been created, by adding this to Grid constructor:

    Bind(wxEVT_GRID_EDITOR_CREATED, [=](wxGridEditorCreatedEvent& event) {
            event.GetControl()->Bind(wxEVT_KEY_DOWN, &Grid::OnKeyDown, this);
        });
    

    This will not handle the initial key press which results in showing the editor in the first place, but from what I understand that would not be necessary here.