如何确定用户最后一次与 matlab GUI 交互?
How to determine the last time a user interacted with the matlab GUI?
在 Matlab 函数中,我想知道用户上次与 Matlab GUI 交互的时间。通过 matlab GUI,我的意思基本上是用户输入命令 window,或在编辑器中输入。
我希望实现的算法本质上是:
如果过了一段时间,该功能将不会抢焦点,而是在后台运行。
如果用户最近进行过交互,则可能 he/she 对结果 "right now" 感兴趣,并且该函数将获得焦点。
这是一个艰难的过程!这是一个建议,仅基于此 undocumented code and persitent variables.
使用命令 window 执行您想要的操作
我使用了两个函数:CW_listen
和 CW_callback
。调用 CW_listen
(或 CW_listen(true)
)开始监听命令 window,而调用 CW_listen(false)
则停止监听。侦听打开时,对命令 window 执行的任何操作都会触发对 CW_callback
.
的调用
下面是两个函数:
function CW_listen(b)
% Default value
if ~exist('b', 'var'), b = true; end
% Get the reference handle to the Command Window text area
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
try
cmdWin = jDesktop.getClient('Command Window');
jTextArea = cmdWin.getComponent(0).getViewport.getComponent(0);
catch
commandwindow;
jTextArea = jDesktop.getMainFrame.getFocusOwner;
end
% Instrument the text area's callback
if b
set(jTextArea,'CaretUpdateCallback',@CW_callback);
else
set(jTextArea,'CaretUpdateCallback',[]);
end
和
function CW_callback(varargin)
% Define a persistent variable
persistent last_call;
if isempty(last_call)
last_call = clock;
else
ts = clock;
Dt = etime(ts, last_call);
% Update the status bar
dt = javaMethod('getInstance', 'com.mathworks.mde.desk.MLDesktop');
if dt.hasMainFrame
dt.setStatusText(['Ellapsed time: ' num2str(Dt) 's']);
end
if Dt>5
fprintf('So long !\n');
last_call = ts;
else
% Do nothing
end
end
我还在状态栏中显示了经过的时间,这对开发代码很有用,并添加了一个非常酷的功能。
您可以用您选择的任何操作替换以秒为单位的时间(此处为 5 秒)和 fprintf('So long !\n');
。请注意,在此 if
语句之外插入任何类型的显示将导致无限显示循环...
目前我不知道如何将其转换为编辑器,但如果您在 Undocumented Matlab 中搜索,您可能会找到如何操作 ;)
在 Matlab 函数中,我想知道用户上次与 Matlab GUI 交互的时间。通过 matlab GUI,我的意思基本上是用户输入命令 window,或在编辑器中输入。
我希望实现的算法本质上是:
如果过了一段时间,该功能将不会抢焦点,而是在后台运行。 如果用户最近进行过交互,则可能 he/she 对结果 "right now" 感兴趣,并且该函数将获得焦点。
这是一个艰难的过程!这是一个建议,仅基于此 undocumented code and persitent variables.
使用命令 window 执行您想要的操作我使用了两个函数:CW_listen
和 CW_callback
。调用 CW_listen
(或 CW_listen(true)
)开始监听命令 window,而调用 CW_listen(false)
则停止监听。侦听打开时,对命令 window 执行的任何操作都会触发对 CW_callback
.
下面是两个函数:
function CW_listen(b)
% Default value
if ~exist('b', 'var'), b = true; end
% Get the reference handle to the Command Window text area
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
try
cmdWin = jDesktop.getClient('Command Window');
jTextArea = cmdWin.getComponent(0).getViewport.getComponent(0);
catch
commandwindow;
jTextArea = jDesktop.getMainFrame.getFocusOwner;
end
% Instrument the text area's callback
if b
set(jTextArea,'CaretUpdateCallback',@CW_callback);
else
set(jTextArea,'CaretUpdateCallback',[]);
end
和
function CW_callback(varargin)
% Define a persistent variable
persistent last_call;
if isempty(last_call)
last_call = clock;
else
ts = clock;
Dt = etime(ts, last_call);
% Update the status bar
dt = javaMethod('getInstance', 'com.mathworks.mde.desk.MLDesktop');
if dt.hasMainFrame
dt.setStatusText(['Ellapsed time: ' num2str(Dt) 's']);
end
if Dt>5
fprintf('So long !\n');
last_call = ts;
else
% Do nothing
end
end
我还在状态栏中显示了经过的时间,这对开发代码很有用,并添加了一个非常酷的功能。
您可以用您选择的任何操作替换以秒为单位的时间(此处为 5 秒)和 fprintf('So long !\n');
。请注意,在此 if
语句之外插入任何类型的显示将导致无限显示循环...
目前我不知道如何将其转换为编辑器,但如果您在 Undocumented Matlab 中搜索,您可能会找到如何操作 ;)