WindowKeyPressFcn and datacursormode on
显示 更早的评论
In the following exampleGUI:

If I set datacursormode on the WindowKeyPressFcn doesn't get called anymore.
I want the press of the spacebar to generate the random point as the pushbutton does. What can be done?
function exampleGUI
S.fh = figure('units','pixels','Pos',[400 400 200 200],...
'Keypress',@fh_kp,'menu','none');
S.pb = uicontrol('Style','PushButton','Pos',[60 165 80 25],...
'String','Random point','Callback',@pb_call);
S.ax = axes('units','pixels','pos',[20 20 160 135],...
'xlim',0:1,'ylim',0:1,'yticklabel','','xticklabel','',...
'box','on');
S.ln = line(-1,-1,'marker','.','markers',15,'markere','r','line','none');
datacursormode on % With this the WindowKeyPressFcn won't get called
% Set new position of the point
function pb_call(varargin)
set(S.ln,'ydata',rand(1),'xdata',rand(1))
end
% Pressing spacebar calls pb_call
function fh_kp(varargin)
if strcmpi(varargin{2}.Key, 'space')
pb_call
end
end
end
EDIT: Implementing datatips w/o setting datacursormode on, I added:
% Get datacursormode object S.dtmode = datacursormode(S.fh); % Added
% Set new position of the point
function pb_call(varargin)
% Remove datacursor
removeAllDataCursors(S.dtmode,S.fh) % Added
set(S.ln,'ydata', rand(1),'xdata',rand(1))
% Create new
S.dtip = createDatatip(S.dtmode, S.ln); % Added
set(S.dtip,'Marker','none') % Added
end
Thanks
Oleg
5 个评论
Matt Fig
2011-2-25
Because of the ongoing Answers problem, I cannot post an answer, only a comment. I don't know of any way to get around the datacursormode blocking the keypressfcn. You might have to make your own datacursor, perhaps something like I showed here:
http://www.mathworks.com/matlabcentral/answers/1308-cursor-line
Walter Roberson
2011-2-25
Probably pretty much the same way you deal with zoom mode disabling the keypressfcn: you have to disable the listener that prevents you changing the keypressfcn, set it to what you want, re-enable the listener. I'll send you the code I use for zoom mode when I'm finished what I'm about to do (if I can find a contact address for you.)
Matt Fig
2011-2-25
I also would be interested in seeing that code, Walter.
Oleg Komarov
2011-2-25
Walter Roberson
2011-2-25
(Code sent to Oleg and Matt.)
I start by installing iCB_zoomkey1 as the keypress callback. When it detects that the appropriate key has been pressed, it turns on zoom and then uses the listener zapper to install iCB_zoomkey2 as the keypress callback. iCB_zoomkey2 is nearly exactly the same as iCB_zoomkey1 (because I want the keys to have the same meaning whether zoom is on or off), but when it detects the key for toggling, it uses the listener zapper routine to turn off zoom and install iCB_zoomkey1 as the figure keypressfcn.
The implementation is not quite as clean as recording the previous keypress function and restoring it, but for my purposes I only needed to toggle between two states, so each state just installs the other state as the keypressfcn.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!