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

14 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by