Clear Filters
Clear Filters

How to know whether contextmenu item was selected by mouse or shortcut?

2 views (last 30 days)
I have a uitable that I add/delete rows from using context menu selections. I have the ability to use the mouse and select the contextmenu or just CTRL+ "N" or "Z" to add/delete rows. Is there a way to know whether the callback for that menu selection is triggered by my mouse vs. the shortcut?
The problem I run into with the code below is if a user selects a cell in row 1, but then uses the mouse+contextmenu on a cell in row 2, the table.selection property is not empty so the code below thinks my user selected row 1.
if ~isempty(app.SequenceTable.Selection)
temp = app.SequenceTable.Selection;
row = temp(1);
else
row = event.InteractionInformation.Row;
end
I previously flipped it so my if statement had the condition below, but that caused a similar problem the other way.
isprop(event,'InteractionInformation')
  1 Comment
Alex
Alex on 8 Mar 2024
One potential improvement that may work is looking at the current modifiers for the main figure:
if ~isempty(get(app.Main_UIFigure,'CurrentModifier'))
temp = app.SequenceTable.Selection;
row = temp(1);
else
row = event.InteractionInformation.Row;
end
This works for normal user input, but if someone decides to hold the CTRL button while they right-click and select an item from the context menu, then it will return the row as the currently selected cell rather than the one that was right-clicked on.

Sign in to comment.

Accepted Answer

Alex
Alex on 8 Mar 2024
Looked around some more and I think my best proposal for this problem is to use the SelectionType of the app's main figure:
if strcmp(app.Main_UIFigure.SelectionType,'normal')
temp = app.SequenceTable.Selection;
row = temp(1);
else
row = event.InteractionInformation.Row;
end

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!