Get "RETURN" key press using "CurrentCharacter" returns and empty string.
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Gabriel
am 17 Jun. 2016
Beantwortet: Geoff Hayes
am 17 Jun. 2016
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?
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 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?
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!