在matlab中检查editbox是否处于活动状态(并且仅当它不是时才评估WindowKeyPressFcn)

时间:2014-09-04 12:58:36

标签: matlab matlab-figure matlab-guide

我希望能够测试编辑框(设置为uicontrol 'style'的{​​{1}})是否已使用此刻(光标是否在该框内/编辑框具有焦点)。

不幸的是,编辑框的'edit'属性似乎始终为'Selected' MathWorks

阻止了它的使用

我对Java解决方案持开放态度(但我还没有尝试过)。

背景

我更广泛的目标是激活特定功能以响应用户按下的键(键盘快捷键),但前提是用户没有在编辑框中键入这些字母。

我使用'off'回调评估键盘快捷键。我考虑过使用editbox的'WindowKeyPressFcn'回调来屏蔽'KeyPressFcn'(通过'WindowKeyPressFcn'setappdata传递内容),但'UserData'似乎总是如此首先评估。

所以现在我试图检测编辑框是否正在以其他方式使用。

2 个答案:

答案 0 :(得分:1)

由于MATLAB uicontrols具有基础Java Swing GUI控件,我可以想到两个" Java解决方案":

  1. Associate a FocusGainedCallback与editbox的Java对象(使用FindJObj - 见下文)(以及可能的其他UI元素),并从此函数更新一些handle \ persistent \ global变量保持当前聚焦的对象句柄。 UndocumentMatlab article on uicontrol callbacks

  2. 中提到了这种类型的回调
  3. 您应该能够在编辑框的基础java对象上使用Java的isFocusOwner()方法(Java documentation of the focus subsystem中提到了此方法)。

  4. 注意:获取java对象的最简单方法是使用简要描述为FindJObj的实用程序here

答案 1 :(得分:0)

另一种更简单的方法是使用GUI图的WindowKeyReleaseFcn回调。这在击键发布后被触发,因此在gco获得当前聚焦的uicontrol的更新值之后执行。 WindowKeyReleaseFcn开头的以下检查可以确定按键是否来自编辑框

function figure1_WindowKeyReleaseFcn(hObject, eventdata, handles)

% gco is already updated by the time we are here to reflect the uicontrol
% which is in focus

if isequal(gco, handles.hEditBox) % Keystroke comes from the edit box
    return
end

% Keystroke does not come from the edit box -- Do something here...
相关问题