This content has been marked as final. Show 10 replies
This seems like such a strange question to me. I can type all day, but if there is nothing set up as a listener then nothing happens. Is that cancelled? As for the mouse, again if there is no code set up for doing something on a specific event it does nothing. Is that cancelled?
What are you really trying to do?
Hmmm, maybe I did not explain it enough. Let's say you have the focus on a TextArea, and you don't want some of the keys you press to reach the TextArea... or there is a ListBox, and you don't want a click to reach the ListBox in a certain situation.
With ActionScript 3, you can easily have a listener on Key, and not dispatch the keystroke (I don't remember the command, something like cancelPropagation() or whatever) if the TextArea in question has the focus.
.. and as I was stating in my first post, usually a keystroke or mouse click will be forwarded to the underlying control. If you are above a Listbox, and you click on it, it will get the mouse event and do it's stuff. As far as I know, Mouse allows you to trap the mouse events, but not cancel them.
With Windows programming, you can easily have a 'hook' on the Application message queue and decide not to dispatch a Windows message to the targetted control if you decide to do so, and the control that has focus will never even know you actually presse a mouse button or key stroke.
EDIT: The magic method in AS3 is cancelDefault(), explained in the AS3 doc here. It works and cancel the default behaviour your event would normally have, and stops it from going down the chain and be processed by the underlying control.
Wouldn't this be controlled by a conditional set anyways? I mean you need to test that the actual event occurs in the first place do you not?
For every action there is an equal or opposite reaction (so says Newton).
As Roth stated, if the listener isn't there, you aren't going to get anything to happen and if the listener exists, in order for flash to know not to run the function tied to it, you'd need to conditionally test.
It seems to me that this function should be applied to certain situation i.e. prototyping or to control listeners in certain namespaces. I can't imagine why you wouldn't just conditionally test to see if the event does one function or another.
I really don't get what you guys mean.
The cursor is on top of a TextArea and I'm typing text. I don't need a listener for the text to be added to the TextArea. Now, when the Enter key is pressed, I don't want the TextArea to grab the key and insert a line in the TextArea. I now need a listener to know the Enter has been pressed and need a way to cancel the event so that the Enter key does not reach the TextArea.
So I really don't get the "if you don't have a listener, nothing is going to happen".
I got a chat application. Now, currently, as a patch, I catch the keyDown and keyUp events and send the message on keyDown (so that Enter key isnt part of the text) and clear the text area on keyUp.
But if you leave your finger on Enter, it will overflow this patch and the enter keys are going to get through.
If I could do like AS3 and cancel the Enter key from even reaching the TextArea, I would not have this problem and would not need any patching.
and adding a key listener that captures your Enter key would allow you this behavior would it not? Force the enter key to always send the text to the chat panel and clear your text area. This is pretty standard anyways.
Nope, it's not going to work, not nicely anyway. If you do it this way, then just leave your finger on the Enter key and see what happens. The control will be overflowed with Enter keys and the listener won't be able to react to all Enter keys so your TextArea will end up being filled up by Enter keys and end up sending the server some Enter keys. So you will be reacting to the Enter key, not cancelling it.
If someone here understands the mechanism of event propagation, and have been using methods like stopPropagation(), stopImmediatePropagation() and preventDefault(), and understand my problem, please let me know if there's some equivalent in AS2 or is AS3 the only way?
I don't know any way to 'prevent' flow of events in as2, because I don't really think there is the same notion of event flow.
And I haven't used the as3 methods yet. But *I think* I have a handle on the concepts.
Anyhow you can do what I think you want to do with this hack for a TextArea. I think I've tried to achieve something similar in the past and came up with something similar to what you described. But I think this is cleaner and doesn't seem to cause the problems you described (although its still a hack) .
Describe the other things you want to achieve and I'll have a go trying to work them out.
Thanks GWD, this is a really interesting approach and will investigate this technique, have not tried the "change" handler yet in my quest for the cleanest hack :-)
I might migrate to AS3 also, because the more I read about it, the more it looks like programmer's haven hehe.
I hadn't tried that approach until now either - perhaps there are better ones.
I think the general approach to consider in as2 is the sequence of events because (I believe) you can't predict or interrupt the flow of a particular event type.
One thing I noticed was that the UIComponent keyDown event ( which I guess is derived from the native Key object anyhow) always occurs before a change event for a TextArea (and presumably for all components with change events?). And there's something in the FocusManager class that seems like it might work with certain components to send the Enter key behaviour to a default push button (which I think is the type of thing you may be looking for here). But I couldn't make that work for a TextArea (although its the first time I used it so perhaps I was doing it wrong).
I've only started to do limited things with as3 to get my feet wet. The event model in as3 is more consistent - or at least appears to be from a distance, for me at this point because I haven't really done much in as3.
My experience with as3's XML was very very pleasing compared to trying to do the same thing even with XPathAPI in as2. But I'm still getting to grips with as3.