Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab &引用;RETURN";使用“按”键;“当前字符”;返回一个空字符串_Matlab_Matlab Figure_Matlab Guide - Fatal编程技术网

Matlab &引用;RETURN";使用“按”键;“当前字符”;返回一个空字符串

Matlab &引用;RETURN";使用“按”键;“当前字符”;返回一个空字符串,matlab,matlab-figure,matlab-guide,Matlab,Matlab Figure,Matlab Guide,我正在尝试编写一个GUI,用户必须在其中输入键盘上的命令(返回或删除)。为此,我编写了一个代码,将“KeyPressFcn”设置为读取用户按下的键。mais的问题是,当用户键入“RETURN”或“DELETE”时,我得到的只是一个空字符串 代码如下: function getKey(axeshandle) fig = ancestor(axeshandle, 'figure'); set(fig, 'KeyPressFcn', @keyRead); uiwait(fig); fu

我正在尝试编写一个GUI,用户必须在其中输入键盘上的命令(返回或删除)。为此,我编写了一个代码,将“KeyPressFcn”设置为读取用户按下的键。mais的问题是,当用户键入“RETURN”或“DELETE”时,我得到的只是一个空字符串

代码如下:

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

你知道怎么解决这个问题吗?

首先,你的问题是它返回当前字符,恰好对于返回键,该字符是一个回车字符(
\r
),所以它看起来像一个空字符串。如果要执行此检查,可以直接与
\r
进行比较,也可以将其与ASCII等效(13)

类似地,delete键返回a(ASCII 127)

您可以通过将当前字符转换为数字(ASCII表示形式)来检查这一点

更好的选择 不要试图获取当前图形的
CurrentCharacter
,而是使用回调的第二个输入(事件数据)来确定按下了哪个键

function keyRead(src, evnt)
    % Access the Key field from the event data
    key = evnt.Key;

    % Compare the value with "return"
    strcmp(key, 'return')

    % Comapre the value with delete
    strcmp(key, 'delete')
end

它们不是空字符串<代码>'CurrentCharacter'返回字符,而不是键。ENTER返回回车符(ASCII 13),DELETE返回DELETE(ASCII 127)。
double(get(src, 'CurrentCharacter'));
function keyRead(src, evnt)
    % Access the Key field from the event data
    key = evnt.Key;

    % Compare the value with "return"
    strcmp(key, 'return')

    % Comapre the value with delete
    strcmp(key, 'delete')
end