How to control multiple pushbutton with keyboard using GUIDE?

3 vues (au cours des 30 derniers jours)
Jahno
Jahno le 31 Août 2016
Commenté : Jahno le 6 Sep 2016
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 commentaires
Geoff Hayes
Geoff Hayes le 5 Sep 2016
Henry - I think that your comment should be an answer. :)
Henry Giddens
Henry Giddens le 5 Sep 2016
Thanks Geoff, I have copied and expanded on this below. (I am new to Matlab Answers!)

Connectez-vous pour commenter.

Réponse acceptée

Henry Giddens
Henry Giddens le 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
  1 commentaire
Jahno
Jahno le 6 Sep 2016
Thanks Henry! This made it work just like I wanted it to.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by