KeyPressFcn and WindowKeyPressFcn not working
조회 수: 35 (최근 30일)
이전 댓글 표시
I have a GUI (using the old figure, not uifigure). The figure has a KeyPressFcn to do some action.
So I have a pushbutton, and after clicking it I want to hide the button, so its callback turns off its visibility ('Visible', 'off').
When that happens, the KeyPressFcn (or WindowKeyPressFcn I also tried it) if the figure stops working. No matter what I do, I can't recover the focus.
So this is what I tried, and nothing worked, even with everything combined:
set(figHandle, 'CurrentObject', figHandle)
set(figHandle, 'CurrentObject', pushbuttonHandle)
figure(figHandle)
gco(figHandle)
gcf(figHandle)
figHandle.Visible = 'off'
figHandle.Visible = 'on'
drawnow
NOTHING WORKS.
Only way to make it work again is clicking the figure. But thats exactly what I DONT want to do. I must not click the figure after clicking the pushbutton, at least before the desired keypress.
However, if I enter debubg mode using breakpoints, then this works:
set(figHandle, 'CurrentObject', figHandle)
figure(figHandle)
But it does not work if not in debug mode. I don't understand this behaviour. I know removing visbility to objects have some strange side effects. But it should never remove permanently focus from a figure.
Why does it work in debugging mode, but not in normal mode?
Matlab R2020b
댓글 수: 2
Voss
2024년 2월 29일
Rather than setting the pushbutton invisible in its callback, try setting its position to something outside the figure, e.g., [-10 -10 1 1], and see if that changes anything.
채택된 답변
Divyajyoti Nayak
2024년 11월 14일 9:10
From what I understand, you want the focus to come back to the parent figure after the button has been clicked, this should give the desired functionality. I could not find any direct function that restores focus to the figure like the ‘focus’ function which works for ‘uifigure’ but I did find a work around.
Here’s a sample code to help you out:
- Initial setup of the GUI
f = figure;
f.KeyPressFcn = @keyPress;
btn = uicontrol(f,'Style','pushbutton','String','Hide','Callback',@btnCallback);
function btnCallback(src, event)
src.Visible = 'off';
- The ‘set’ function can be used to set the ‘enable’ property to ‘off’ which disables the button. This removes the focus from the button.
set(src, 'Enable', 'off');
- ‘drawnow’ updates the figure bringing the focus back onto it
drawnow update;
- If the button is still needed, it can be enabled without losing focus from the figure using the ‘set’ function again:
set(src, 'Enable', 'on');
end
function keyPress(src,event)
disp("key pressed");
end
For more information on this workaround, you can check out this MATLAB Answer post:
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!