Using a keypress to activate a button

13 visualizaciones (últimos 30 días)
Ben Hornsby
Ben Hornsby el 27 de Mzo. de 2012
Editada: Jia Tsing Ng el 30 de Mayo de 2014
I have seen several posts on this topic but I am a Matlab novice and am still having trouble. I have a larger program that currently uses pushbuttons for responses but I am modifying it for children and want them to also be able to push a key.
I am using Matlab Version 7.0.4.365 (R14) Service Pack 2.
As a start I used guide to create a simple GUI with a single pushbutton. When I use the mouse to click on the pushbutton it changes the figure color in a random way. I then copied the pushbutton callback that guide created e.g., jnk('pushbutton1_Callback',gcbo,[],guidata(gcbo)) and pasted that into the keypressfcn section of the figure using the property editor. My thought was that I would be able to now use any key or the mouse to activate the button code.
This works great IF I only use the keys to activate the button. Any key works. However, once I use the mouse to press the button the keys no longer work (except for the spacebar for some reason).
Because of the task I want to be able to use both the mouse click and, if possible, keypresses during a single run so the current setup isn't useful (i.e., only use the keypress).
I also tried creating a separate function to call using keypressfcn but that did the same thing.
Any suggestions would be greatly appreciated.
Thanks,
Ben

Respuestas (2)

Jan
Jan el 27 de Mzo. de 2012
If a button is activated with the mouse, it gets the focus. Subsequent keyboard events are forwarded to the button's keypress functions in consequence.
To solve this problem, the figure must get the focus inside the callbacks of the button. Actually figure(gcf) should do this, but this does not work since Matlab 5.3. A workaround is to disable the button temporarily inside its callback:
set(ButtonH, 'Enable', 'off');
drawnow;
set(ButtonH, 'Enable', 'on');
Using the WindowsKeyPressFcn works in modern Matlab versions only.
  1 comentario
Ben Hornsby
Ben Hornsby el 28 de Mzo. de 2012
Thanks Jan,
Sorry for the long post. Not sure if this detail is needed or not...
We went ahead and upgraded to Matlab 2012a V7.14.0.739 and pasted the callback function in the "WindowsKeyPressFcn" box using propertyeditor. Now with a simple gui as described in my original post everything works fine. I even added a second button to make sure I could press it and have it work as well as using my keypress to activate a different button. All was well here.
Unfortunately when I try to implement this in my main program I continue to have the same problem. This main program is used to measure speech understanding (and reaction times). It plays out a sound file of someone saying a name, a color and a number. If the subject hears the "target" number then they currently use the mouse to click on a "Stop" button (this is where I want to allow them to push a key instead).
When you click the stop button it makes the "Stop" button inactive (to keep from multiple presses) and "Enables" 8 other buttons (each with a Name) and then enters a while loop to wait for the subject to press one of the 8 "Name" buttons. After the subject clicks on a name button the program sets all name buttons to "inactive" (to prevent second choices) and exits the while loop.
It then "enables" 4 other "Color" buttons and enters a second while loop. It then waits for subjects to select one of the 4 color buttons. Once a color button is pressed, all color buttons also become inactive and the "Stop" button is "enabled" again to allow the process to repeat.
When I put the "Stop" button callback in the WindowsKeyPressFcn spot (or create a separate function) it works to like using the mouse to press stop IF I press a key first (If I try the mouse first and then the keypress the keypress isn't recognized). Regardless if I use the mouse to select the name and color the keypress to stop no longer works.
I did look at your answers 555 and 33224. Adding the following code to the Name and Color button callbacks did not help.
uicontrol(hObject);
set(hObject,'Enable','on');
drawnow;
set(hObject,'Enable','inactive');
Any other suggestions?
Thanks,
Ben

Iniciar sesión para comentar.


Jia Tsing Ng
Jia Tsing Ng el 30 de Mayo de 2014
Editada: Jia Tsing Ng el 30 de Mayo de 2014
I've tried this and using Jan Simon's suggestion of
set(ButtonH, 'Enable', 'off');
drawnow;
set(ButtonH, 'Enable', 'on');
at the end of the button callback function works! THANKS A LOT!
I initially tried copying the assigning a button's KeyPressFcn to the same KeyPressFcn for the figure. This works as well, but is dangerous especially if you have (as I did!) a 'spacebar' shortcut. Pressing the space button will activate whatever button that is in focus, then do what your space shortcut is.
So I'd still recommend Jan's suggestion. Although there SHOULD be something more straightforward. Anybody from Mathworks reading this?!

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by