是否可以使用鼠标向下和向上事件来模拟鼠标单击?
之前的一个问题引起了我的思考。是否可以通过先触发 MOUSE_DOWN 再触发 MOUSE_UP 来模拟 MouseEvent.CLICK 的触发。
根据 Adobe 的文档。 “...要发生单击事件,它必须始终按发生顺序跟随这一系列事件:mouseDown 事件,然后是 mouseUp。这两个事件的目标对象必须相同;否则单击事件不会发生在 mouseDown 或 mouseUp 事件之间随时可以发生任意数量的其他鼠标事件; http://help.adobe .com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html#event:click
从我的测试来看,CLICK 事件不是从 ActionScript 3 事件队列构造的。或者代码有问题吗?
请参阅:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(backgroundColor="0xFFFFFF", frameRate="30", width="800", height="600")]
public class Test extends Sprite
{
private var spr:Sprite = new Sprite();
public function Test()
{
trace("Test()");
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(e:Event):void
{
trace("init()");
spr.graphics.beginFill(0xFF0000);
spr.graphics.drawRect(0,0,200,80);
spr.graphics.endFill();
addChild(spr);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
spr.addEventListener(MouseEvent.CLICK, onClick);
}
private var tick:int = 0;
private function onEnterFrame(e:Event):void
{
if (tick == 1) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN,true,false));
if (tick == 2) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP,true,false));
if (tick == 3) spr.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true,false));
tick++;
}
private function onClick(e:MouseEvent):void
{
trace("onClick() MouseEvent.CLICK dispatched");
}
}
}
我应该得到两个“onClick()”事件,而不是一个。
A previous question has gotten me thinking. Is it possible to simulate a MouseEvent.CLICK to get fired by just firing first a MOUSE_DOWN followed by a MOUSE_UP.
As per Adobe's doumentation.
"... For a click event to occur, it must always follow this series of events in the order of occurrence: mouseDown event, then mouseUp. The target object must be identical for both of these events; otherwise the click event does not occur. Any number of other mouse events can occur at any time between the mouseDown or mouseUp events; the click event still occurs." http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html#event:click
From my tests it shows that the CLICK event is NOT constructed from the ActionScript 3 event queue. Or is something wrong with the code?
See:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(backgroundColor="0xFFFFFF", frameRate="30", width="800", height="600")]
public class Test extends Sprite
{
private var spr:Sprite = new Sprite();
public function Test()
{
trace("Test()");
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(e:Event):void
{
trace("init()");
spr.graphics.beginFill(0xFF0000);
spr.graphics.drawRect(0,0,200,80);
spr.graphics.endFill();
addChild(spr);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
spr.addEventListener(MouseEvent.CLICK, onClick);
}
private var tick:int = 0;
private function onEnterFrame(e:Event):void
{
if (tick == 1) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN,true,false));
if (tick == 2) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP,true,false));
if (tick == 3) spr.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true,false));
tick++;
}
private function onClick(e:MouseEvent):void
{
trace("onClick() MouseEvent.CLICK dispatched");
}
}
}
I should get TWO 'onClick()' events instead of one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不能的原因是所有三种类型都是由插件创建的,并且彼此不依赖。
当您按下交互式对象时,MouseEvent.MOUSE_DOWN 就会被触发。
一旦释放鼠标,MouseEvent.MOUSE_UP 就会被触发,它不依赖于在同一 InteractiveObject 中启动 MOUSE_DOWN 的鼠标单击。
仅当两个事件发生在同一对象中且光标在鼠标按下和鼠标松开之间的时间间隔内没有离开该对象时,才会触发 MouseEvent.CLICK。
所以你可以看到有一个简单的情况,甚至两个,其中 MOUSE_DOWN 和 MOUSE_UP 都被触发,但 CLICK 没有被调用,因为该事件不是 CLICK。
此外,当只需要一个鼠标事件时,只需分派 MouseEvent.CLICK 事件的能力就无需通过触发多个鼠标事件来创建虚假的用户交互。如果必须跟踪鼠标按下侦听器中单击的每个对象,以检查鼠标向上是否也应该触发单击,那就很尴尬了。当 ROLL_OUT 事件发生时,您还需要删除引用。
简而言之,CLICK 事件确实比 MOUSE_DOWN 和 MOUSE_UP 事件多得多。它还管理事件在同一显示对象上连续发生而无需离开该对象。当这种情况发生时,并且仅当这种情况发生时,闪存才会调度 CLICK 事件。
The reason you cannot is that all three types are created by the plugin, and are not dependent upon each other.
MouseEvent.MOUSE_DOWN is fired as soon as you press on an interactive object.
MouseEvent.MOUSE_UP is fired as soon as you release the mouse, it does not depend upon the mouse click having started a MOUSE_DOWN within the same InteractiveObject.
MouseEvent.CLICK will only be fired when both events take place in the same object without the cursor leaving the object between the itnerval of mouse down and mouse up.
So you can see that there is a simple case, or two even, in which both MOUSE_DOWN and MOUSE_UP are fired, but CLICK is not called because the event is not a CLICK.
Furthermore, the ability to simply dispatch a MouseEvent.CLICK event makes it unnecesasry to create fake user interaction by firing multiple mouse events, when only one is called for. It would be awkward to have to track each object clicked in the mouse down listener, in order to check that the moue up should also trigger a click. You would also need to remove references when a ROLL_OUT event happened.
In short, the CLICK event is really a lot more than the MOUSE_DOWN and MOUSE_UP events. It is also managing that the events happened in succession and on the same display object without leaving the object. When that happens, and ONLY when that happens flash dispatches a CLICK event.
这是不可能的,这段代码:
输出只是:
按下键盘上的某个键后
it's impossible, this code:
outputs just:
after a key is pressed on the keyboard
不确定闪存的这一点。但是当我学习windows MFC时,却一无所知。我所做的是先发送 WM_MOUSEDOWN,然后在短暂的延迟后发送 WM_MOUSEUP。它确实有效,并且按钮的视觉状态也发生了变化 - 无需单击任何鼠标!
not sure about this for flash. But when i was learning windows MFC and was clueless. What i did was that I sent WM_MOUSEDOWN followed by WM_MOUSEUP after a short delay. It did work and the button's visual states also changed - without any mouse click!
如果我明白你在问什么,那么答案是肯定的。
我很好奇我是否过度简化了这里的解决方案,特别是在阅读您的代码尝试之后。 “模拟”鼠标点击非常简单。下面的代码在单击对象时输出两个“我已被单击”语句,而当您按下鼠标然后移出对象边界时则不会输出两个“我已被单击”语句。
这个类扩展了我在舞台上创建的 MovieClip,因此我不必编写盒子代码。
在您的代码中,我认为问题在于您试图通过“onEnterFrame”运行事件侦听器分派,该事件每帧只会触发一个事件 - 永远不会触发两个事件。另外,在测试了您的代码之后,我不确定您试图使用“tick”变量来完成什么,因为您从未重置它。因此,MOUSE_UP 事件触发一次(当tick = 2 时,除非您按住鼠标),然后其他调度都不会再次触发。
If I understand what you're asking then the answer is YES.
I am curious if I have over simplified the solution here especially after reading your code attempt. It is very simple to "simulate" a mouse click. The code below outputs two "I have been clicked" statements when the object is clicked and none when you mouse down then move out of the objects boundaries.
This class extended a MovieClip I created on the stage so I didn't have to code a box.
In your code I believe the problem is that you are trying to run event listener dispatches through an "onEnterFrame" which will only ever fire one event per frame - never two. Also after testing your code I'm not sure what your were trying to accomplish with the "tick" variable because you never reset it. So the the MOUSE_UP event fires once (when tick = 2, unless your holding the mouse down) then none of the other dispatches will ever fire again.
不太清楚为什么每个人都制作这些复杂的尖峰。或者也许我只是在监督一些事情。
无论如何,这里有一个非常简单的方法来检查它:
答案是,“不,这是不可能的”......
并且可能应该紧随其后的是一个反问题:为什么会我想使用一系列 mousedown 和 mouseup 来模拟单击事件,当我也可以调度单击事件时?
我是一个面临生存危机的按钮吗?
或者只是出于好奇而提出的问题?但你知道那只猫发生了什么……对,但也许它是薛定谔的猫,这意味着在这个完全不相干的思路结束时,好奇心确实杀死了猫,但没有杀死猫。
除非猫已经脱离了盒子。
那么它可能就被浪费了。
你看,跳出框框思考可能会害死你。
Not quite sure why everybody's making these complex spikes. Or maybe I'm just overseeing something.
Anyway, here's a very easy way to check it:
And the answer would be, "no, it's not possible"...
...and probably should be followed by a counter-question: WHY would I want to simulate a click event using a sequence of mousedown and mouseup, when I can dispatch a click event just as well?
Am I a button, having an existential crisis?
Or is it a question out of mere curiosity? But you know what happened to the cat... Right, but maybe it's schrödinger's cat, which means that at the end of this completely irrelevant train of thought, curiosity REALLY did and did NOT kill the cat.
Except if the cat's out of a box.
Then it probably just got wasted.
See, thinking out of the box can get you killed.