フィルターのクリア

Get "RETURN" key press using "CurrentCharacter" returns and empty string.

14 ビュー (過去 30 日間)
Gabriel
Gabriel 2016 年 6 月 17 日
回答済み: Geoff Hayes 2016 年 6 月 17 日
Dear all,
I am tryiing to write a GUI in which the user must enter the command (RETURN or DELETE) from the keyboard. For that purpose, I wrote a code where I set the 'KeyPressFcn' to read the key pressed by the user. The mais problem is that when the user types "RETURN" or "DELETE" all I get is an empty string.
Here is the code:
function getKey(axeshandle)
fig = ancestor(axeshandle, 'figure');
set(fig, 'KeyPressFcn', @keyRead);
uiwait(fig);
function keyRead(src, callback)
key = get(fig, 'CurrentCharacter');
strcmp(key, 'return')
class(key)
end
end
Any idea on how can I solve this?

採用された回答

Geoff Hayes
Geoff Hayes 2016 年 6 月 17 日
Gabriel - to be clear, are you expecting the user to type in RETURN or DELETE or press the return or delete keys on the keyboard? The following line of code
key = get(fig, 'CurrentCharacter');
will just return the character of the pressed key. For a button like return, key will be an empty character.
If you want to capture the input from the keyboard, then I would change your callback to
function keyRead(src, event)
key = event.Key;
fprintf('The key being pressed is: %s\n', key);
end
By the way, I'm not sure why you have the uiwait or are using the f instead of fig. What is the intent of the former, and is the latter a typo?

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by