From: Kara on
Hi All,
I am a beginner in GUI programming and I have a question about a callbacks.
So I am writing a GUI code that will respond to pressing and releasing a space bar (or any other key, doesn't matter). So I want the program to do one task while the space bar is being pressed and other task while no key is pressed. I want to do such a loop:

function pushbutton1_Callback(hObject, eventdata, handles)
set(handles.pushbutton1,'string','test started')

while handles.time <400 (I want the test to last e.g. 400s)
handles.atten2=handles.atten2 - 2;
guidata(hObject, handles)
pause(1) %I want this action being done each 1 sec

if pushbutton1_KeyPressFcn was called
handles.atten2=handles.atten2 + 2;
guidata(hObject, handles)
end

end

function pushbutton1_KeyPressFcn(hObject, eventdata, handles)

So I want the code to do something like tracking - decrease the aaten2 value each 1 sec when nothing is pressed, and increase atten2 value each 1 sec when a key is pressed. I figured out that I can use this callback KeyPressFcn and when it is called the while loop start to increase the attent but don't know how to establish that KeyPressFcn was called (and this info needs to be updated very fast)

Any helpful ideas?

PS I have tried KeyPressFcn and KeyReleaseFcn as figure properties but this didn't work at all..