A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Loss of mouseOver focus when using setInterval and setFocus

  1. #1
    Junior Member
    Join Date
    Apr 2005
    Posts
    7

    Loss of mouseOver focus when using setInterval and setFocus

    I am ripping my hair out due to what seems to me bizarre behavior by flash.

    I have a demo that simulates a real device so has a number of buttons. These can be pressed to give different text input depending on whether they are just pressed or the user presses and holds on them.

    The problem is that I want to put the focus back to the input Box and have a flashing cursor at the end. But every time I do this the button that has the mouse over it looses focus. As in mouseOver is lost. If you then move the mouse a tiny bit the mouseOver focus comes back. This problem means that the user has to do an extra click to get the mouse focus back and it makes it feel like the demo is broken. Here is my code. It is based on the idea that there is a button called "num4" and an input box called "testInput" on the stage.

    num4.onRelease = function() {_root.t9timer1()}

    function t9timer1() {
    var repStore:Number = 0;
    keyInterval = setInterval(keyRep, 100);
    function keyRep()
    {
    repStore ++ ;
    if(repStore == 10)
    {
    clearInterval(keyInterval);
    testInput.text = testInput.text.substr(0) + "g"
    Selection.setFocus("testInput");
    Selection.setSelection(testInput.length, testInput.length);
    }
    }
    }

    I have to use setInterval as I need maximum control over the timings. Something I cannot have from code based on workarounds using a movieClip with code embedded in it.

    I hope you can help!

  2. #2
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    Use movie clips instead of buttons, and manually script the rollover action. Something along these lines is what I'd try first:
    Code:
    myButton.onRollOver = function() {
        this.gotoAndStop("overStateFrame");
    }
    
    myButton.onRollOut = function() {
         if( !this.hitTest(_xmouse, _ymouse, true)) {
              this.gotoAndStop("outStateFrame");
         }
    }
    RollOver and rollOut might get confused by setFocus. If so, try putting code in onMouseMove instead.
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  3. #3
    Junior Member
    Join Date
    Apr 2005
    Posts
    7
    Thanks, but this still does not help. As the problems exists when the mouse is not being moved I am not sure how onMouseMove can help. The mouse stays in the same place during the setFocus code. Any other ideas?

  4. #4
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    If you set the focus to a text field, the button no longer has the focus, so will no longer appear in it's over (or down) state by default. So, don't use a button -- use a movie clip instead, and write the code to make the clip change between different states exactly when you want it to.
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  5. #5
    Junior Member
    Join Date
    Apr 2005
    Posts
    7
    Sorry my previous post was far too brief I have replaced the buttons with movieclips and used code like rollOut, roolOver, onPress, onRelease, onDragOut etc to simulate all the possible things a button does. But the problem still exists. I even changed the demo so that instead of setInterval I have a movieClip that plays and when the head hits the end some code runs to change focus. This too suffers with the same problem. The second setFocus works the entire movie looses the ability to recognise that the mouse is over anything (reflected by the cursor no longer being a hand and instead being the standard system cursor). It will only start focusing buttons again if I wiggle the mouse. Something that is not good enough for my project. Thanks for putting in the time to help. It really is appreciated.

  6. #6
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    Here's an example of how I would try to do it. See if this helps at all. It can be rather tricky!
    Attached Files Attached Files
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  7. #7
    Junior Member
    Join Date
    Apr 2005
    Posts
    7
    Sadly I cannot use that type of code. I am trying to replicate the exact behavior of text entry via a mobile phone keyboard. Take the 2 key. You can hold it down for half a second to get '2'. Or you can press it once for 'a', twice for 'b' and so on. After one second the cursor appears so you can enter the next character. So I cannot have the focus appearing just based on mouse move.

    I have attached a cut down version with just a '2' key from the telephone. In code ther is only one array, but in the real app each key has a seperate array. this is why there is the odd code "_root["keyNumber" + keyNum1][0]". To see the problem just press the phone key once and after a second you will see it looses focus. I have only been coding 3 months so am still getting the hang of writing readable actionscript. Just ask if you want to know why I am doing anything.
    Attached Files Attached Files

  8. #8
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    The same approach should work. Do not use rollOver since that will be undone when focus is set elsewhere. Instead, I think onMouseMove is really what you need.

    Use onMouseMove code to send the key movieclip to its highlighted state, as in my code. This will have nothing to do with rollOver, so it will not be affected when the focus goes to the textfield.
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  9. #9
    Junior Member
    Join Date
    Apr 2005
    Posts
    7
    Nope, this still does not work. All this does is give the illusion of a focused button. It looks like the button is focused. However, once the cursor appears in the text field I need to again click the mouse or wiggle the cursor to get focus (as in the button will now do something). I then click again and the button does its job. It is like setFocus takes away focus from the entire movie until it is clicked on again or the cursor is wiggled.

  10. #10
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    No, setFocus just takes the focus away from the button when it gives focus to the text field. For your application, you need to do explicitly what we usually rely on onRollOver and other events handlers to do. So, in the function that you call using setInterval, you need to explicitly check:


    is the mouse still over the button? If no, clear interval and return.

    has the mouse been released yet? If yes, clear interval and return.

    if we survive those tests, then increment the letter displayed.

    --------

    Another approach would be to change the content of the text field, but keep the focus on the button.
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  11. #11
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173

    P.S.

    P.S. I think Flash MX 2004 Pro comes with some kind of pre-built FocusManager class. That might be another route to try.
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  12. #12
    Junior Member
    Join Date
    Apr 2005
    Posts
    7
    okay. Thanks a lot for all your help. I will see if I can still get an event listner for the mouse click. If I can then I will just have some code to remember what the last button to have focus was and perform that action. If that fails I will give up :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center