ReoGrid provides three keyboard events on the worksheet for handling key presses inside cells.
Namespace
using unvell.ReoGrid;
using unvell.ReoGrid.Events;
using unvell.ReoGrid.Interaction;
Events
| Event | EventArgs | Description |
|---|---|---|
BeforeCellKeyDown | BeforeCellKeyDownEventArgs | Raised before built-in key handling. Set IsCancelled = true to suppress default behavior |
AfterCellKeyDown | AfterCellKeyDownEventArgs | Raised after built-in key handling |
CellKeyUp | CellKeyDownEventArgs | Raised when a key is released |
EventArgs Properties
CellKeyDownEventArgs
Base class for all keyboard event arguments.
| Property | Type | Description |
|---|---|---|
KeyCode | KeyCode | The key that was pressed (includes modifier flags) |
Cell | Cell | The cell where the key event occurred |
CellPosition | CellPosition | Position of the cell |
BeforeCellKeyDownEventArgs
Extends CellKeyDownEventArgs.
| Property | Type | Description |
|---|---|---|
IsCancelled | bool | Set to true to cancel the default operation |
KeyCode Enum
The KeyCode enum (flags) represents keyboard keys. Common values:
| Value | Description |
|---|---|
None | No key |
Back | Backspace |
Tab | Tab |
Enter | Enter |
Escape | Escape |
Space | Spacebar |
PageUp | Page Up |
PageDown | Page Down |
End | End |
Home | Home |
Left, Up, Right, Down | Arrow keys |
Insert | Insert |
Delete | Delete |
D0–D9 | Number keys 0–9 |
A–Z | Letter keys |
NumPad0–NumPad9 | Numpad keys |
F1–F24 | Function keys |
Modifier Flags
| Value | Description |
|---|---|
Shift | Shift modifier (65536) |
Control | Ctrl modifier (131072) |
Alt | Alt modifier (262144) |
Modifiers | Bitmask to extract modifiers |
Check for modifiers using bitwise AND:
if ((e.KeyCode & KeyCode.Control) == KeyCode.Control)
{
// Ctrl is held
}
Examples
Cancel Default Key Behavior
Prevent Enter from moving the selection:
sheet.BeforeCellKeyDown += (s, e) =>
{
if (e.KeyCode == KeyCode.Enter)
{
e.IsCancelled = true;
}
};
Detect Key Combinations
sheet.BeforeCellKeyDown += (s, e) =>
{
if (e.KeyCode == (KeyCode.Control | KeyCode.S))
{
// Ctrl+S pressed
SaveWorkbook();
e.IsCancelled = true;
}
};
Handle Key Release
sheet.CellKeyUp += (s, e) =>
{
Console.WriteLine($"Key released: {e.KeyCode} at cell {e.CellPosition}");
};
Handle After Key Down
sheet.AfterCellKeyDown += (s, e) =>
{
if (e.KeyCode == KeyCode.Delete)
{
Console.WriteLine($"Cell {e.CellPosition} content cleared");
}
};
Remarks
Keyboard events are not raised when a key press causes the cell to enter edit mode. Character keys (A–Z, 0–9, symbols) typically trigger edit mode on editable cells. In that case, only BeforeCellKeyDown fires (before edit mode starts). Use Cell Editing events to handle input during editing.
Related Topics
- Cell Editing — Edit events (BeforeCellEdit, AfterCellEdit, CellEditCharInputed)
- Mouse Events — Cell mouse events
- Hot Keys — Built-in keyboard shortcuts