필터 지우기
필터 지우기

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 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