Change keyboard focus in MATLAB GUI

I am trying to allow the user to press return or space to enter values, so whenever they press these keys, no matter where the crusor is, a certain callback must be intiated. What happens is that the GUI loses focus whenever I press on the editable fields and start writing values, but if I pressed on the button manualy the enter and space keys regain focus.
the code block where I define these keyboard keys is
function keyPressCallback(source,eventdata)
keyPressed = eventdata.Key;
if strcmpi(keyPressed,'space') | strcmpi(keypressed, 'return')
uicontrol(app.FillTableButton);
snapshot_callback(app.FillTableButton,[]);
end
end
Please anyone has an idea how to solve this issue?
Thank you

댓글 수: 5

Geoff Hayes
Geoff Hayes 2020년 5월 11일
Adham - how is this callback used? Which object is it a callback for? I suspect that you just added this callback function to your code without trying to "attach" it to a particular object. I see that your code references app which isn't an input parameter to this function (so that will cause an error) so you must be using App Designer. Please provide more details.
Adham Elkhouly
Adham Elkhouly 2020년 5월 11일
편집: Adham Elkhouly 2020년 5월 11일
Thanks for replying Yes I am using app designer.
The problem: I have two editable-blanks. I want the user to push a certain button, whenever they are writing on any of these fields, by pressing enter or space,
The function: This is a separate function that I added by pressing on the plus sign on the mid-left because, first, I decided that it will be easier to make, but, since you replied, I am telling you what I am really trying to do.
Callback: The callback that the function initiates is for button (app.filltablebutton) and the field names are app.XvalueEditfield and app.YvalueEditField.
I am very sorry if my question was vague (I am very new to MATLAB GUI) Thank you
Geoff Hayes
Geoff Hayes 2020년 5월 11일
You may need to create a separate callback function for each edit text field. See the ValueChangingFcn property from edit field properties for details.
Adham Elkhouly
Adham Elkhouly 2020년 5월 11일
Can you please show me how and where I can do so?
Thank you
Hi,
Since you mentioned you are new to MATLAB's GUI, you can refer the following documentation to help you gain more clarity in adding calbacks in Appdesigner.

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

답변 (1개)

BhaTTa
BhaTTa 2024년 11월 27일

0 개 추천

Hi @Adham Elkhouly, In MATLAB GUIs, handling global keyboard shortcuts like pressing "Return" or "Space" can be tricky, especially when focus shifts between different UI components. The issue you're encountering is due to the GUI focus changing when you interact with different controls, such as editable fields. To ensure that your key press callback works regardless of focus, you need to set up the callback at the figure level and ensure that it persists across interactions.
Here's a step-by-step approach to address this:
Step-by-Step Solution
  1. Set the KeyPressFcn at the Figure Level: Ensure that the key press callback is set at the figure level, so it captures key presses regardless of which UI component currently has focus.
  2. Use Logical OR Operator Correctly: Use || instead of | for logical operations in your conditions.
  3. Ensure Consistent Case: Ensure that the case of variable names is consistent (e.g., keyPressed vs. keypressed).
  4. Focus Management: Consider managing focus explicitly if needed.

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 5월 9일

답변:

2024년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by