App designer key press function

조회 수: 87 (최근 30일)
Benedict Low
Benedict Low 2020년 11월 4일
답변: Aniket 2024년 11월 21일 4:58
Hi,
I am using App Designer, and I would like to use the key press function, such that pressing the left arrow and right arrow on the keyboard, performs the same functions as clicking the buttons "previous" and "next" respectively. My code, which doesn't work, is as follows:
function UIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'rightarrow'
NextframeButtonPushed(app, event);
case 'leftarrow'
PreviousframeButtonPushed(app, event);
end
end
Was wondering if anybody knows where the problem lies. Thanks in advance for your time.
  댓글 수: 2
Ramya Dodla
Ramya Dodla 2020년 11월 4일
Benedict Low
Benedict Low 2020년 11월 9일
편집: Benedict Low 2020년 11월 9일
Thank you Ramya for responding.
I realised that the code didn't work because of the focus. The key press function only worked when the figure had focus, but not the children. Which means I had to always ensure that the focus was not on any child object, before pressing the keys.
A better solution was therefore the window key press function, because the code executes as long as the figure or any child object has focus.
function UIFigureWindowKeyPress(app, event)
key = event.Key;
switch key
case 'rightarrow'
NextframeButtonPushed(app, event);
case 'leftarrow'
PreviousframeButtonPushed(app, event);
end
end

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

답변 (1개)

Aniket
Aniket 2024년 11월 21일 4:58
Hi,
This issue can happen when the current focus is set on a child component in UI figure.
The "KeyPressFcn" Callback executes when the user presses a key while the UI figure itself has focus. It does not trigger if the focus is on a child component like an edit field, unless that component is disabled. This callback is suitable when you want key events to be recognized only when the figure is in focus and that the interactions with child components do not interfere.
On the other hand, the "WindowKeyPressFcn" Callback activates when a key is pressed while either the UI figure or any of its child components has focus. This makes it ideal for scenarios where you want the callback to respond regardless of where the focus is within the figure. It eliminates the need to manually adjust focus away from child components.
To know more about callbacks in UI Figure, kindly follow the documentation below:
I hope this resolves the issue.

카테고리

Help CenterFile Exchange에서 Data Exploration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by