keypress callback not working for a second called UIFigure

조회 수: 6 (최근 30일)
Christian Mier
Christian Mier 2023년 2월 6일
답변: Christian Mier 2023년 2월 12일
Hello, I have a UIFigure which opens a second UIFigure, but in this second UIFigure the keypress callback is not working. My app is very long so I just attached the code where the main UIfigures calls the other, the start function of the second UIfigure, and my keypress callback function. I already tried to focus on the figure with: figure(app.UIFigure), but it didn't work.
Thanks in advance.
%this code is in the main UIFigure
function goButtonPushed(app, event)
app.GraficasWindow = Graficas(app,app.OscCh,app.magnitudes); %Graficas is the name of my second uiFigure
end
%this is the starting function of second UIFIgure
function startupFcn(app, PDatorWindow, OscCh, magnitudes)
%bunch of other irrelevant things
end
%and this is the callback function
function UIFigureKeyPress(app, event)
key = event.Key;
display(key)
end
  댓글 수: 1
Christian Mier
Christian Mier 2023년 2월 6일
I just noticed that the keypress callback does not work when the app.cursor is enabled. So now my question is: How can I enable the keypress callback and the app.cursor at the same time?

댓글을 달려면 로그인하십시오.

채택된 답변

Christian Mier
Christian Mier 2023년 2월 12일
The problem is that once the datacursor mode is enable, the KeyPressFcn is blocked. So, if you want to continue using the KeyPressFcn and at the same time move the data cursor, you can use the following code:
function UIFigure2KeyPress(app, event)
key = event.Key;
app.cursor = datacursormode(app.UIFigure2); %this line can alse be in the startupFcn
if key(1)=='r' %"r" stands for rightarrow
app.cursor.Figure.CurrentObject.Children.DataIndex=app.cursor.Figure.CurrentObject.Children.DataIndex+1;
elseif key(1)=='l' %"l" stands for leftarrow
app.cursor.Figure.CurrentObject.Children.DataIndex=app.cursor.Figure.CurrentObject.Children.DataIndex-1;
end
end

추가 답변 (1개)

Jasvin
Jasvin 2023년 2월 10일
A possible workaround for this can be trying to change the focus to a UI element that’s within the second UI Figure via the focus() method (https://www.mathworks.com/help/matlab/ref/matlab.ui.figure.focus.html?s_tid=doc_ta). And from here you can try out options like setting the active figure via figure(app.UIFigure) again or if it's feasible, change the KeyPress callback from the UI Figure to this UI component that’s within the second UI Figure.
  댓글 수: 1
Christian Mier
Christian Mier 2023년 2월 12일
Thanks for your answer, but it does not solve my problem. The real is problem is that keypress function and enabling the app.cursor cannot coexist at the same time. I have answered with a different approach that help for my specific situation.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by