CurrentCharacter properties not working as expected

18 ビュー (過去 30 日間)
Luca Amerio
Luca Amerio 2016 年 11 月 22 日
コメント済み: Luca Amerio 2016 年 11 月 25 日
Hi everybody.
if I run the code
h=figure;
h.CurrentCharacter=char(i);
disp(double(h.CurrentCharacter))
for i=1,2...127 I obtain what I expect, that is to display the same value I stored in "CurrentCharacter". After 127 however things start getting weird. For i=128 for example it returns me 65408, for 256 it returns an empty variable, while for 257 it returns 1.
I know 127 are the ASCII character, but is there a way to avid this behavior?
----------------------------
The reason I want to set
h.CurrentCharacter=65000;
is that in the middle of a script I have a "" loop with
while true
waitfor(fig, 'CurrentCharacter')
switch fig.CurrentCharacter
case 13 %User pressed Enter
case 27 %User pressed Esc
etc...
end
I would like to set a popup menu with the callback
dataTypePopup=uicontrol(popupFig,'Style','popup',...
'Callback',{@(obj,ev,fig) set(fig,'CurrentCharacter',char(65400+obj.Value)),fig}
in order to trigger an option in the while loop. Of course I want to set a character that the user will NEVER trigger by itself and char(65401) was actually a good candidate...

採用された回答

Jan
Jan 2016 年 11 月 23 日
編集済み: Jan 2016 年 11 月 23 日
Using 'CurrentCharacter' to trigger an event is a really bad idea. This figure property contains the value of the last pressed key of the keyboard during the figure is the active window. Do not write values there.
Store informations either in the 'UserData' or 'ApplicationData' of the figure. Then you can let the WindowsKeypressFcn write the current character (if a key is pressed and only if a key is pressed) to the 'UserData' also:
hFig = figure('WindowKeyPressFcn', @myKeyPress);
dataTypePopup = uicontrol(hFig, 'Style', 'popup',...
'Callback',{@(obj,ev,fig) set(fig, 'UserData', 65400+obj.Value), fig}
...
function myKeyPress(hFig, EventData)
set(hFig, 'UserData', double(EventData.Key))
Now in the main routine:
waitfor(fig, 'UserData')
switch fig.UserData
  1 件のコメント
Luca Amerio
Luca Amerio 2016 年 11 月 25 日
Thank you Jan! You are always incredibly usefull!
Your solution is way better than mine.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by