How to focus on 'edit text' callback by WindowKeyPressFcn in GUIDE.

1 view (last 30 days)
I have a gui that includes an 'edit text' callback (queryet_1).
function queryet_1_Callback(hObject, eventdata, handles)
% hObject handle to queryet_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of queryet_1 as text
% str2double(get(hObject,'String')) returns contents of queryet_1 as a double
input = get(hObject,'String');
To add a keyboard shortcut I use the following code.
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata structure with the following fields (see FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
k = eventdata.Key;
if strcmp(k,'f2')
queryet_1_Callback(hObject, eventdata, handles)
end
However, it gives the following error, refering to the line “input = get(hObject,'String');”
Error using hg.figure/get
The name 'String' is not an accessible property for an instance of class 'figure'.
My questions is why WindowKeyPressFcn does not work for 'edit text' callback while it deos for others like 'bushbottun'?

Accepted Answer

Geoff Hayes
Geoff Hayes on 23 Jun 2016
Ali - look at how you are calling queryet_1_Callback from within the figure1_WindowKeyPressFcn function
if strcmp(k,'f2')
queryet_1_Callback(hObject, eventdata, handles)
end
In the above, hObject is the handle to figure1 and not the queryet_1 text box. hObject always refers to the handle to the "source" of the callback (so the control that the callback is assigned to). Instead, pass the handle to the queryet_1 as
if strcmp(k,'f2')
queryet_1_Callback(handles.queryet_1, eventdata, handles)
end
Try the above and see what happens!

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!