'String' value of edit box only updating after Enter or losing focus

7 views (last 30 days)
This problem has been discussed here before but all of the answers are not working for me.
The concept: I have a GUI with some edit boxes, programmed in Matlab 2012b. I want to immediately update a plot when the user enters a new numeric value in these edit boxes. The logical solution is programming a Keypressfcn callback for the edit box, which reads out the 'String' value of the box and updates the plot with this newly entered value. The problem with the String value of edit boxes is that it only updates after pressing Enter (which is not really logical for the user) or losing focus of the edit box.
I tried different proposed solutions: # 1. Putting a pause in the keypressfcn callback. Does not solve the problem. # 2. Let Java simulate an Enter key press in the keypressfcn callback. Problem is that this Enter is registered as a key press, which fires another key prees callback -> infinite loop. # 3. Program the keypressfcn callback to lose and quickly regain focus of the edit box. This works, but the problem is that during the regaining of the focus, the contents of the edit box are selected, so if you want to enter a number higher than 10 you have to manually deselect the contents by clicking with the mouse. Extremely cumbersome to use.
That's all I could find, any other options?

Answers (2)

Michiel
Michiel on 22 Apr 2013
I fixed it by using the following code i found somewhere and modified a little:
if ~isequal(eventdata.Key,'return')
import java.awt.Robot;
import java.awt.event.KeyEvent;
robot=Robot;
robot.keyPress(KeyEvent.VK_ENTER);
pause on
pause(0.01)
robot.keyRelease(KeyEvent.VK_ENTER);
pause(0.01)
pause off
% any other code you want to execute after keypress
end
Probably not the most beautiful code, but the functionality is what matters to me.
  3 Comments
Abhishek Vaidya
Abhishek Vaidya on 16 Mar 2019
Edited: Abhishek Vaidya on 16 Mar 2019
Made some changes to make it work better. I use it for using the edit as a search box. with the changes i made, you can search with Shift pressed (and thus use special characters).
Let me know if this works for you.
function edit_KeyPressFcn(hObject,eventdata,handles)
if isequal(eventdata.Key,'shift')
return
end
shiftIsPressed = ismember('shift',get(gcf,'currentModifier'));
if ~isequal(eventdata.Key,'return')
import java.awt.Robot;
import java.awt.event.KeyEvent;
robot=Robot;
pause on
if shiftIsPressed
robot.keyRelease(KeyEvent.VK_SHIFT);
end
pause(0.01)
robot.keyPress(KeyEvent.VK_ENTER);
pause(0.01)
robot.keyRelease(KeyEvent.VK_ENTER);
pause(0.01)
if shiftIsPressed
robot.keyPress(KeyEvent.VK_SHIFT);
end
pause(0.01)
pause off
else
% your code here
end
end
TADA
TADA on 16 Mar 2019
Edited: TADA on 16 Mar 2019
I Will Try That Out When I Get The Chance..
Also I Changed Your pause on/off to
pauseState = pause('on');
% All That Useful Code
pause(pauseState); % Return Original Pause State
To Maintain The State Of The Pause Operation. Turned Out To Be Crucial For Some Of My Unit Tests

Sign in to comment.


Jan
Jan on 17 Apr 2013
Edited: Jan on 17 Apr 2013
You find a lot of matching solutions in the FileExchange as dialogs to input passwords. Then the edit field is disabled by setting the 'Enable' property to 'inactive'. And adding the new characters is done by the figure's KeyPressFcn, which allows to start the callback directly without pressing Enter.
  1 Comment
Michiel
Michiel on 17 Apr 2013
Thanks a lot for the reply. Apparently they all work via the principle of figuring out which key was pressed and adding that to the old(existing) value of the edit box. This however does not work well when you select the complete contents of the edit box with the mouse and enter a new value. It then still is added to the old value which you actually wanted to delete.
Can you elaborate a bit more on "setting the 'Enable' property to inactive"? I do not follow you there...

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!