我希望能够测试编辑框(设置为uicontrol
'style'
的{{1}})是否已使用此刻(光标是否在该框内/编辑框具有焦点)。
不幸的是,编辑框的'edit'
属性似乎始终为'Selected'
, MathWorks
我对Java解决方案持开放态度(但我还没有尝试过)。
我更广泛的目标是激活特定功能以响应用户按下的键(键盘快捷键),但前提是用户没有在编辑框中键入这些字母。
我使用'off'
回调评估键盘快捷键。我考虑过使用editbox的'WindowKeyPressFcn'
回调来屏蔽'KeyPressFcn'
(通过'WindowKeyPressFcn'
或setappdata
传递内容),但'UserData'
似乎总是如此首先评估。
所以现在我试图检测编辑框是否正在以其他方式使用。
答案 0 :(得分:1)
由于MATLAB uicontrols具有基础Java Swing GUI控件,我可以想到两个" Java解决方案":
Associate a FocusGainedCallback
与editbox的Java对象(使用FindJObj - 见下文)(以及可能的其他UI元素),并从此函数更新一些handle \ persistent \ global变量保持当前聚焦的对象句柄。 UndocumentMatlab article on uicontrol
callbacks。
您应该能够在编辑框的基础java对象上使用Java的isFocusOwner()
方法(Java documentation of the focus subsystem中提到了此方法)。
答案 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...