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
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
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
Matt Fig 2011 年 2 月 25 日
I also would be interested in seeing that code, Walter.
Oleg Komarov
Oleg Komarov 2011 年 2 月 25 日
@Walter: I came across your solution. I'll try it asap: https://groups.google.com/group/comp.soft-sys.matlab/msg/db42cf51392b442a?hl=en
@Matt: very nice.
I also found that using makedatatip w/o setting explicitly datacursormode on, works well.
Walter Roberson
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 ExchangeIntroduction to Installation and Licensing についてさらに検索

質問済み:

2011 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by