是否可以在WindowButtonMotionFcn中使用多个回调? [英] Is it possible to use multiple callbacks in the WindowButtonMotionFcn?

查看:176
本文介绍了是否可以在WindowButtonMotionFcn中使用多个回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,它为构建一个图添加了功能。这个类为 WindowMouseMotion 事件创建一个监听器;然而,要使这个事件触发,我不得不为图的 WindowButtonMotionFcn 属性添加一个虚拟回调函数。我首先检查这个属性是否已经填充。如果不是,那么我将它设置为一个无效的哑回调函数。

I created a class which adds functionality to a figure on construction. This class creates a listener for the WindowMouseMotion event; however, to get this event to fire I had to add a dummy callback function for the figure's WindowButtonMotionFcn property. I first check if this property is already populated. If it isn't then I set it to a dummy callback function that does nothing.

不是检查属性是否已经设置,对任何现有的回调虚假回调?回调属性是否可以调用多个函数?

Instead of checking if the property is already set or not, can I simply add this dummy callback to any existing callbacks? Is it possible for a callback property to call multiple functions?

EDIT

当使用 handle.listener 方法处理下面给出的 WindowButtonMotionEvent 事件时,请务必使用 eventdata.CurrentPoint 以访问当前鼠标位置。该图的 CurrentPoint 属性在以这种方式处理 WindowButtonMotionEvent 事件之前不会更新。

When using the handle.listener approach to handle the WindowButtonMotionEvent event given below, be sure to use eventdata.CurrentPoint to access the current mouse position. The CurrentPoint property of the figure does not get updated before handling the WindowButtonMotionEvent event in this manner.

推荐答案

相关文章可以在Yair Altman的Undocumented MATLAB博客上找到,来自客户博客Matt Whitaker。您所指的是回调链,并在博客中引用:

A related article can be found on Yair Altman's Undocumented MATLAB blog, from guest blogger Matt Whitaker. What you are alluding to is callback chaining, and quoting from the blog:


坦率地说, b $ b以前处理callback
chaining,我宁愿用叉子戳眼睛

Frankly, having written code previously that handles callback chaining, I would rather poke myself in the eye with a fork!



<幸运的是,在这篇文章中似乎有一个替代解决方案。使用发布在那里的代码片段,我能够得到一个函数来执行鼠标移动,而不必设置'WindowButtonMotionFcn'。我添加了一个监听器到当前的数字,像这样:

Luckily, there appears to be an alternative solution in that article. Using a snippet from the code posted there, I was able to get a function to execute on mouse movement without having to set the 'WindowButtonMotionFcn'. I added a listener to the current figure like so:

myListener = handle.listener(gcf,'WindowButtonMotionEvent',...
                             @(hSource,eventData) disp('hello'));

当我显示 hello 在窗口中移动鼠标。

And the message hello was displayed when I moved the mouse in the window.

这篇关于是否可以在WindowButtonMotionFcn中使用多个回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆