Clear Filters
Clear Filters

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

14 views (last 30 days)
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?

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Jun 2016
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?

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!