How can I use keypress callback from a uitable in a uifigure?
13 views (last 30 days)
Show older comments
I am trying to use the KeyPressFcn callback for my main UIFigure in appdesigner, but it is not called when the user has a cell in a UITable selected. Is there any way to allow this so I could allow the user to press delete to execute a delete row function for example?
I have tried using the UITable's key press callback property, but that throws an error. Apparently that callback is not supported in UITables that are decendants of UIFigures.
Thank you in advance for your help and suggestions!
0 Comments
Answers (1)
Rahul
on 15 Apr 2025
The row of the 'uitable' not deleting when 'KeyPressFcn' callback is added to the 'uifigure' and the 'uitable' is selected is expected behaviour as the current focus is on the 'uitable' and hence it does not register the keypress when 'uitable' is selected. The desired functionality of deleting a row from 'uitable' after pressing the 'delete' key can be done in the following ways:
Adding a 'WindowKeyPressFcn' callback on the 'uifigure'. This would register the keypress even if the 'uifigure' is not the component in focus. The following code can be added to the callback to achieve the desired result:
function UIFigureWindowKeyPress(app, event)
if strcmp(event.Key, 'delete')
% Get the selected indices from the UITable
selection = app.UITable.Selection;
% Extract the row indices from the selection
selectedRows = unique(selection(:, 1));
if ~isempty(selectedRows)
% Convert the table data to a cell array for manipulation and delete the selected row
data = app.UITable.Data;
data(selectedRows, :) = [];
app.UITable.Data = data;
end
end
end
In MATLAB version R2020a the 'uitable' component did not have a 'KeyPressFcn' callback. From MATLAB version R2021a this callback was introduced. Hence you can upgrade your MATLAB version to recent release which would have the 'KeyPressFcn' of the 'uitable' callback to be working as expected. The code given above can then be used inside the 'KeyPressFcn' callback of 'uitable' as well, it would achieve the same desired result.
The following MATLAB Answers can also be helpful:
The following MathWorks documentation can be referred to know more:
'WindowKeyPressFcn' (uifigure): https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-WindowKeyPressFcn
Thanks.
0 Comments
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!