App Designer KeyPress callback for a Tree

7 次查看(过去 30 天)
Is there a work around for making a KeyPress callback when a Tree is active within the App Designer? I would like to remove selected tree nodes when the user presses delete but there are no keypress callbacks on the Trees. There is a figure wide keypress callback but I dont see a way to determine if the tree is active or one of the edit fields are active.

采纳的回答

Allen
Allen 2020-1-6
Chris,
As you mentioned, App Designer Trees do not yet have a KeyPress callback. While the UIFigure does have this callback, there are a few quirks to get around in order to use it in conjunction with the Tree. The user must select the node(s) in the tree and this click somewhere on the figure outside of the Tree and not on the title bar of the figure in order to inactivate, but not deselect the node(s). This will allow the UIFigure's KeyPress callback to active. I used the following code to look for the 'delete' button to be pressed and to delete all selected nodes within the Tree.
% If delete key is pressed, then delete the selected node(s)
if strcmp(event.Key,'delete')
arrayfun(@delete,app.MyTree1.SelectedNodes)
end
This works okay when only a single Tree exists in your UIFigure that you what to apply this ability to, but less so when there are multiple Trees. If you prefer to still use the KeyPress callback to delete nodes on multiple trees, then you could use a dual keypress such as by first selecting and holding aa number key (1 - for 1st tree, 2 - for 2nd tree, etc.) before pressing delete.
% If '1' key is held and then delete key is pressed, then delete the selected node(s) from the 1st tree
if strcmp(event.Modifier,'1') && strcmp(event.Key,'delete')
arrayfun(@delete,app.MyTree1.SelectedNodes)
end
% If '2' key is held and then delete key is pressed, then delete the selected node(s) from the 2nd tree
if strcmp(event.Modifier,'2') && strcmp(event.Key,'delete')
arrayfun(@delete,app.MyTree2.SelectedNodes)
end
% etc..
However, it might be more useful to add a pushbutton near each of your Trees to perfrom this task versus using the KeyPress callback. This would simply the user interface by making it much clear how to delete from which Tree. A pushbutton will also eliminate the need to first select outside of the Tree before the UIFigure callback can be accessed.
Good luck and hope this helps.
-Allen
  1 个评论
Chris
Chris 2020-1-6
Thanks Allen,
I do have a 'delete' button but muscle memory is strong. I have been developing and using the app for nearly a year and I still hit the delete key several times a week. :-)
I had not known about or thought to look for a modifier field; I have added a WindowKeyPressFcn callback looking at "ctrl+delete" then I call my delete function. Seems to work, will see how I like it. (The delete function has a popup conformation.)
Will accept your answer tuesday if nothing better is posted. thanks.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by