MATLAB Answers

WindowKeyPressFcn and datacursormode on

12 views (last 30 days)
Oleg Komarov
Oleg Komarov on 25 Feb 2011

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 Comments
Walter Roberson
Walter Roberson on 25 Feb 2011
(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.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!