我想在数字处于缩放模式时能够使用WindowKeyPressFcn。这个问题最近在这里被问到Overriding ctrl-z behavior in matlab zoom mode,但我刚刚做了一个最小的例子来证明同样的问题(我会在他们的帖子上写评论,但还没有足够的代表)。有谁知道我们缺少什么?
function listenWhileZooming
%% Main problem:
% I want any key press to change the color of the plot, even when in Zoom
% mode. I tried to override the mode manager, but don't see any effect.
%%
%% Create and then hide the GUI as it is being constructed
f = figure('Visible','off','units','normalized','Position',[0.1 0.1 0.5 0.5],'windowkeypressfcn',@colorSwap);
%% Override mode manager
hManager = uigetmodemanager(f);
try
set(hManager.WindowListenerHandles, 'Enable', 'off'); % HG1
catch
[hManager.WindowListenerHandles.Enabled] = deal(false); % HG2
end
set(f, 'WindowKeyPressFcn',@colorSwap);
%% Plot something
plot(1,1,'bo')
%% Make the GUI visible
f.Visible = 'on';
%% Key press callback
function colorSwap(source,eventData)
myLine = findobj(source,'type','line');
if all(myLine.Color == [0 0 1])
plot(1,1,'ro')
else
plot(1,1,'bo')
end
end
end
答案 0 :(得分:0)
我知道现在已经很晚了,但这是你失踪的一块。
我假设(在您的代码中)f
是数字句柄并且f.WindowKeyPressFcn
已由您设置。
%% Fix
Button = findall(f, 'Tag', 'Exploration.ZoomIn');
OldClickedCallback = Button.ClickedCallback;
Button.ClickedCallback = @(h, e) FixButton(f, OldClickedCallback, f.WindowKeyPressFcn);
Button = findall(f, 'Tag', 'Exploration.ZoomOut');
OldClickedCallback = Button.ClickedCallback;
Button.ClickedCallback = @(h, e) FixButton(f, OldClickedCallback, f.WindowKeyPressFcn);
function Result = FixButton(Figure, OldCallback, NewCallback)
eval(OldCallback);
hManager = uigetmodemanager(Figure); % HG 2 version
[hManager.WindowListenerHandles.Enabled] = deal(false);
Figure.KeyPressFcn = [];
Figure.WindowKeyPressFcn = NewCallback;
Result = true;
end
在您设置f.WindowKeyPressFcn
后,它会被缩放处理程序重置。因此,我们劫持两个缩放按钮(您可以对Pan
或Rotate
执行相同操作)以首先调用原始回调,然后重新应用修复。另外,不要忘记删除KeyPressFcn
。它非常优雅,因为你可以对所有按钮使用相同的FixButton
。