'String' value of edit box only updating after Enter or losing focus
Show older comments
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
on 22 Apr 2013
3 Comments
TADA
on 20 Feb 2019
thanks for that solution.
I'm afraid that in 2017b this is still an issue
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
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
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.
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!