How to update editbox uicontrol string during keypressfcn?

Hi,
I have an editbox uicontrol that has a KeyPressFunction that would need to read what has been typed into the uicontrol. How can I make the KeyPressFunction to read the typed text? If I use
get(gcbo,'String')
it returns the string from before pressing any keys / since the last Callback event (in other words whatever the string was after I last pressed enter or clicked away from the editbox).
I want for the editbox to read the string when I press the downarrow key and act according to what has been written.
Thanks in advance, EK

 採用された回答

Grzegorz Knor
Grzegorz Knor 2012 年 1 月 13 日

0 投票

Try this code as a workaround:
function test
close all
uicontrol('Style','Edit','String','matlab','KeyPressFcn',@clbck)
function clbck(src,evnt)
if isequal(evnt.Key,'downarrow')
import java.awt.Robot;
import java.awt.event.KeyEvent;
robot=Robot;
robot.keyPress(KeyEvent.VK_ENTER);
pause(0.01)
robot.keyRelease(KeyEvent.VK_ENTER);
disp(get(src,'String'));
end

1 件のコメント

Eero Kuusi
Eero Kuusi 2012 年 1 月 18 日
Thank you, this workaround worked perfectly!

サインインしてコメントする。

その他の回答 (2 件)

ALLAEDINE AH
ALLAEDINE AH 2012 年 4 月 10 日

0 投票

Hi guys! What if i wanted to enter only numbers, ie: 39 OR 1.2 ?
Mitchell
Mitchell 2012 年 5 月 23 日

0 投票

This is straightforward, on the callback of the edit control do:
number = str2double(get(@myControl,'String'));
if(isnan(number))
set(@myControl,'String',defaultValue);
end
Also, if the str2double command was successful you could update defaultValue to number so that every time a non-numeric is entered the value will be replaced by the last numeric value.

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by