Reputation: 1025
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.
Upvotes: 0
Views: 723
Reputation: 1987
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.
Upvotes: 0
Reputation: 22688
You need to use a custom editor (probably just deriving from the standard wxGridCellTextEditor
) and bind to the wxEVT_CHAR
event of the editing control created in its Create()
.
Notice that if you want to handle the initial key press, which results in showing the editor in the first place, you need to override wxGridCellEditor::StartingKey()
instead, as this key press happens before the editing control is shown.
Upvotes: 1