hedgehogues
hedgehogues

Reputation: 237

matlab's GUI KeyPressFcn

I am using the KeyPressFcn method for the edit box to test whether enter is pressed. I can use call_back but there is not event_data call_back function.

If I press one time on the button of Enter, than text doesn't rewrite, but if I double time on the button of Enter (speedly), than text rewrite.

What reasons this behaviour?

function WriteData(val, name, ind)
     global solver;
     switch ind
         case {14, 15}
             value = strcat('@(t)', val);      
         case 16
             value = strcat('@(x)', val);
         case {17, 18}
             value = strcat('@(x,t)', val);
     end
     eval(strcat('solver.', name, ' = ', num2str(val) ) );

function edit1_KeyPressFcn(hObject, eventdata, handles)
     val = get(hObject, 'String');
     [~, ~, var] = GetActiveData(handles.listbox1); 
     ind = get(handles.listbox1, 'Value'); 
     if (strcmp(eventdata.Key, 'return') )
         WriteData(val, var, ind );
     end

Upvotes: 0

Views: 712

Answers (1)

Tim Adams
Tim Adams

Reputation: 156

According to the documentation found in UIControl Properties (http://www.mathworks.com/help/matlab/ref/uicontrol-properties.html;jsessionid=49b9dc47d9f964ec95a4fe2cc9f3),

This callback function executes when the uicontrol object has focus and the user presses a key. If you do not define a function for this property, MATLAB passes key presses to the parent figure. Repeated key presses retain the focus of the uicontrol, and the function executes with each key press. If the user presses multiple keys at approximately the same time, MATLAB detects the key press for the last key pressed.

More simply put, the Callback will be called when you press enter the first time and the KeyPressFcn will be applied on the second press.

Upvotes: 1

Related Questions