How to control multiple pushbutton with keyboard using GUIDE?
1 view (last 30 days)
Show older comments
I've been trying to make a piano in MATLAB (without sound, thus far) using my keyboard to play the piano instead of the mouse. So far I've been able to perform the "execution" of one pushbutton, but when I try to use another one it will not execute.
% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(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)
switch eventdata.Key
case 'c'
uicontrol(handles.C)
C_Callback(handles.C,[],handles)
case 'd'
uicontrol(handles.D)
D_Callback(handles.D,[],handles)
end
This is how the KeyPressFcn looks like in my m.file. I haven't yet done the whole piano, only the C and D key. As I mentioned, I can press one button, and it will be "marked", but I can't do both C and D in one run.
Thanks in advance!
5 Comments
Henry Giddens
on 5 Sep 2016
Thanks Geoff, I have copied and expanded on this below. (I am new to Matlab Answers!)
Accepted Answer
Henry Giddens
on 5 Sep 2016
Is it be possible that the focus is no longer on the figure and is instead on the pushbutton associated to the previous key?
This is presumably caused by the line
uicontrol(handles.C)
If you want to keep this behaviour (i.e. the push button is highlighted when you call uicontrol(handles.C)), then each push button should also have an associated KeyPress_Fcn, which is the same as the keypress_fcn displayed in the original question. You can set the key press function for each key either automatically GUIDE (a new function will be created for each individual key), manually in GUIDE (copy the syntax for the figure1_KeyPressFcn from the property editor so that the same function is executed for all push buttons), or programatically.
Henry
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!