Callback on radiobutton change with arrow keys
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm sure it has been answered in the past, but I am too stupid to find it on here... Anyway, I have the a piece of code similar to the following example:
function listen_to_radiobutton
figGUI = figure('Name','My GUI');
hButtongroup = uibuttongroup('Position',[0.03 0.26 0.25 0.16],'SelectionChangeFcn',@doMyStuff);
hTextfield = uicontrol(figGUI,'Style','text','Units','normalized','String','xyz','Position',[0.5 0.26 0.25 0.16]);
radioMyoption1 = uicontrol(hButtongroup,'Style','radiobutton','String','Option 1','Units','normalized','Position',[0.08 .55 0.84 0.35],'Value',1);
radioMyoption2 = uicontrol(hButtongroup,'Style','radiobutton','String','Option 2','Units','normalized','Position',[0.08 .1 0.84 0.35]);
listen_to_options = addlistener(hButtongroup,'SelectedObject','PostSet',@doMyStuff);
end
function doMyStuff(varargin)
if radioMyoption1.Value == 1
hTextfield.String = 'abc';
else
hTextfield.String = 'xyz';
end
end
This works like charm as long as I click on the radio buttons. The doMyStuff function is executed any time I change between options 1 and 2, as it should be. However, if I am too lazy to use the pointer pad on my laptop and cycle through the options of the GUI with the tab and arrow keys on my keyboard, the active radio button changes but the function is not executed - only if I select the button and press the space bar afterwards to actively select the option.
Is there any way around this, i.e. either have the selection to change when cycled with arrow keys, or to disable the access by keyboard. The software should be distributed to non-experts and this could create a lot of confusion if one of them decides to use the keyboard for accessing the GUI...
Thanks in advance for all your help!!!!!!!!!!!!!
댓글 수: 1
Rik
2019년 2월 22일
There are two problems. The first is that your example doesn't work (which I fixed in the code below). The second one is that need to capture the keypresses from the figure, which is strange. Either method should work, so I'm curious if someone knows the reason why the change function is not triggered. I suspect this has to do with how uicontrols in general work (only an enter key (and space for non-text elements) will trigger the callback).
function listen_to_radiobutton
figGUI = figure('Name','My GUI');
hButtongroup = uibuttongroup('Position',[0.03 0.26 0.25 0.16]);
hTextfield = uicontrol(figGUI,'Style','text','Units','normalized','String','xyz','Position',[0.5 0.26 0.25 0.16]);
radioMyoption1 = uicontrol(hButtongroup,'Style','radiobutton','String','Option 1','Units','normalized','Position',[0.08 .55 0.84 0.35],'Value',1);
radioMyoption2 = uicontrol(hButtongroup,'Style','radiobutton','String','Option 2','Units','normalized','Position',[0.08 .1 0.84 0.35]);
%either SelectionChangedFcn or a listener will work
%property name changed with R2014b
set(hButtongroup,'SelectionChangedFcn',@doMyStuff)
set(hButtongroup,'SelectionChangeFcn',@doMyStuff)
addlistener(hButtongroup,'SelectedObject','PostSet',@doMyStuff);
h=struct;
h.hTextfield=hTextfield;
h.radioMyoption1=radioMyoption1;
h.radioMyoption2=radioMyoption2;
guidata(figGUI,h)
end
function doMyStuff(hObject,event)
try
%in case of SelectionChangedFcn
h=guidata(hObject);
catch
%in case of listener
h=guidata(event.AffectedObject.Parent);
end
if h.radioMyoption1.Value == 1
h.hTextfield.String = 'abc';
else
h.hTextfield.String = 'xyz';
end
end
답변 (1개)
Agnish Dutta
2019년 2월 26일
편집: Agnish Dutta
2019년 2월 26일
If the user presses a key on a 'UIControl' or 'Table' component, the callback does not execute unless the Enable property is set to 'off'.
Please refer to the 'Keyboard Callbacks' and 'Window Callback' sections of the following document:
https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#d120e330487
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!