Using KeyPressFcn to initiate the callback?

Hi, I have a function under a button callback, including ginput and other functions. However, at the moment, the button needs to be pressed over and over to run that ginput function. how can I initiate the callback with the KeyPressFcn?
Example:
function button_Callback(hObject, eventdata, handles)
[x, y] = ginput(1);
sum = x + y;
set(handles.display,'string',sum);
I'm not sure if this above program works completely (may have an error between strings and numbers) but that's not the point, this is a dummy program. If I'd want to run this small bit of script with a key press, how could I do that? Thank you!

 Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Jun. de 2013
function mykeypress_Callback(hObject, eventdata)
%invoke the same thing as pressing the button
handles = guidata(hObject);
button_Callback(hObject, eventdata, handles);

4 comentarios

Jake
Jake el 8 de Jun. de 2013
Say I wanted to press j+1, how would I structure it? I'm still not understanding the code - sorry I'm a beginner with Matlab.
Walter Roberson
Walter Roberson el 8 de Jun. de 2013
I do not know what you mean about pressing "j+1" ? Do you mean that you want to run the code if the last three keys the user pressed were 'j' and '+' and '1', and when the '1' is encountered the code should be started immediately (rather than waiting to see whether the user might have been typing 'j+107.3' ?)
Jake
Jake el 9 de Jun. de 2013
I meant that the user would press J and 1 at the same time. When these buttons are pressed in conjunction, the script should run.
Walter Roberson
Walter Roberson el 9 de Jun. de 2013
On standard keyboards, J and 1 at the same time would be a bit tricky, as J is a shifted character, but if you are holding down the shift key then (US keyboards) the 1 would become ! (exclamation mark.) It is not impossible to generate the combination, though: caps-lock j 1 would act to shift the j to J but would not shift the 1 to !

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Jun. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by