We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Key Capturing & Handling

I am trying to capture the arrow keys in order to increment my data by a certain value. This works perfectly well using CurrentCellPreviewKeyDown. However, the arrow keys are also used for navigation. I am setting Handled = true after I captured the arrow keys but still the navigation does not stop.

How do I handle these keys correctly, so that the selection is not going to move when hitting the arrow keys?

5 Replies

KB Kanimozhi Bharathi Syncfusion Team June 24, 2016 11:54 AM UTC

Hi Marco Studer, 
 
Thank you for contacting Syncfusion Support. 
 
You can cancel the navigation while pressing arrow keys in GridControl by using CurrentCellPreviewKeyDown Event of GridControl like below code example 
 
 
grid.CurrentCellPreviewKeyDown += grid_CurrentCellPreviewKeyDown; 
 
void grid_CurrentCellPreviewKeyDown(object sender, GridCellKeyEventArgs args) 
{ 
     if(args.Key == Key.Down || args.Key == Key.Up || args.Key == Key.Left || args.Key == Key.Right) 
        args.Cancel = true; 
} 
 
Regards 
Kanimozhi B 



MS Marco Studer June 28, 2016 10:05 AM UTC

Thank you very much, work like a charm!


KB Kanimozhi Bharathi Syncfusion Team June 29, 2016 04:05 AM UTC

Hi Marco Studer, 
Thank you for your update. 
Regards 
Kanimozhi B 



KR Krzysztof January 7, 2020 08:23 PM UTC

How to get the same but only on an active cell, e.g. to block the change of value in CellType = "IntegerEdit"?


AR Arulpriya Ramalingam Syncfusion Team January 8, 2020 09:45 AM UTC

Hi Krzysztof, 
 
Thank you for using syncfusion products. 
 
CellType of current cell can be checked to restrict the up/down or right/left key functions and the current cell can be moved based on corresponding pressed key by using the MoveCurrentCellWithArrowKey() method. Please make use of below code and sample. 
 
Example code 
 
//Event subscription 
grid.CurrentCellPreviewKeyDown += grid_CurrentCellPreviewKeyDown; 
//Event customization 
private void grid_CurrentCellPreviewKeyDown(object sender, GridCellKeyEventArgs args) 
{ 
    GridCurrentCell currentCell = grid.CurrentCell; 
    if (grid.Model[currentCell.RowIndex, currentCell.ColumnIndex].CellType == "IntegerEdit" && 
        (args.Key == Key.Down || args.Key == Key.Up || args.Key == Key.Left || args.Key == Key.Right)) 
    { 
        args.Cancel = true; 
        //To Move current cell as per other cells arrow key behavior. 
        grid.MoveCurrentCellWithArrowKey(args.KeyEventArgs); //NOTE: this can  be added only if needed. 
    } 
} 
 
 
Please get back to us, if you need any assistance. 
 
Regards, 
Arulpriya 


Loader.
Up arrow icon