10 Replies Latest reply on Aug 23, 2007 10:56 PM by Greg Dove

    Any way to *cancel* a Mouse or Key event with ActionScript 2?

    William0275
      Hello,

      I know it's possible with ActionScript 3, but was wondering if it were possible with ActionScript 2. I would like to be able to trap a Mouse or Keyboard event so that the event is not propagated to the underlying control, as if the actual mouse click or key stroke never happened. Is there any way?
        • 1. Re: Any way to *cancel* a Mouse or Key event with ActionScript 2?
          Rothrock Level 5
          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?
          • 2. Re: Any way to *cancel* a Mouse or Key event with ActionScript 2?
            William0275 Level 1
            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.
            • 3. Any way to *cancel* a Mouse or Key event with ActionScript 2?
              William0275 Level 1
              .. 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.
              • 4. Re: Any way to *cancel* a Mouse or Key event with ActionScript 2?
                SymTsb Level 2
                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.
                • 5. Any way to *cancel* a Mouse or Key event with ActionScript 2?
                  William0275 Level 1
                  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.
                  • 6. Re: Any way to *cancel* a Mouse or Key event with ActionScript 2?
                    SymTsb Level 2
                    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.
                    • 7. Re: Any way to *cancel* a Mouse or Key event with ActionScript 2?
                      William0275 Level 1
                      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?
                      • 8. Any way to *cancel* a Mouse or Key event with ActionScript 2?
                        Greg Dove Level 4
                        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.



                        • 9. Re: Any way to *cancel* a Mouse or Key event with ActionScript 2?
                          William0275 Level 1
                          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.
                          • 10. Re: Any way to *cancel* a Mouse or Key event with ActionScript 2?
                            Greg Dove Level 4
                            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.