A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: [AS3] key/focus problem

  1. #1
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188

    [AS3] key/focus problem

    Don't laugh, after I just said how wonderful AS3 is, I ran into a problem. It's probably easy to fix though.

    Okay, so I have dialogs, which you close by pressing a button. So, they just say
    Code:
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDown);
    Which is called after they have been parented.
    Code:
    private function keyDown(e:KeyboardEvent):void {
    	this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.keyDown);
    	/* etc */
    }
    But, well, sometimes the game seems to randomly stops receiving key events, especially in the IDE player with 'disable keyboard shortcuts', but I've also seen it in the web plugin. I have to click on the game again so it receives events again, so it seems to have lost focus, but there is no reason for that.

    I also have a Key class somewhere which mimics Key.isDown functionality, I doubt it has any influence but it looks like this :

    Code:
    package com.crossconscious {
    	import flash.display.Sprite;
    	import flash.display.Stage;
    	import flash.events.Event;
    	import flash.events.KeyboardEvent;
    	
    	/**
    	* ...
    	* @author Sven Magnus
    	*/
    	public class Key {
    		
    		public static var stage:Stage;
    		private static var keys:Object = {};
    		
    		public static function init(stage:Stage):void {
    			Key.stage = stage;
    			Key.stage.addEventListener(KeyboardEvent.KEY_UP, Key.keyUp);
    			Key.stage.addEventListener(KeyboardEvent.KEY_DOWN, Key.keyDown);
    			Key.stage.addEventListener(Event.DEACTIVATE, Key.clearKeys);
    		}
    		
    		private static function keyUp(e:KeyboardEvent):void {
    			if (e.keyCode in Key.keys) {
    				delete Key.keys[e.keyCode];
    			}
    		}
    		
    		private static function keyDown(e:KeyboardEvent):void {
    			Key.keys[e.keyCode] = true;
    		}
    		
    		public static function isDown(keyCode:int):Boolean {
    			if (Key.keys[keyCode]) return true;
    			return false;
    		}
    		
    		private static function clearKeys(e:Event):void {
    			Key.keys = [];
    		}
    		
    	}
    	
    }

    It's probably something very obvious, but I just don't know what it is. Any ideas?

  2. #2
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Also, just did a check, and the stage doesn't actually loose focus. I put a text field and put some text in in the Event.DEACTIVATE event of the stage, and it didn't happen, only when I actually click outside of the swf. So it still has focus, but my event callers are not called when a key gets pressed.

  3. #3
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Okay, I found an ugly hack to fix this, and it's not hurting my game at all, so I'm satisfied, but I would still like to know why this was happening.

    (my hack involves using a sprite, and setting stage.focus to that sprite each frame, and using that sprite for events, which isn't ideal but it's the only thing I tried that seems to work).

  4. #4
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    as3 is great isn't it ?

    I posted about the very same thing on the blog the other day, and I had to do the stage.focus=stage; thing.

    Squize.

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    I dont see anything wrong with that code.

    do you have warnings mode and strict mode on, maybe something is failing silently, like "this.stage" is undefined.
    lather yourself up with soap - soap arcade

  6. #6
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Quote Originally Posted by Squize
    as3 is great isn't it ?

    I posted about the very same thing on the blog the other day, and I had to do the stage.focus=stage; thing.

    Squize.
    I said don't laugh Oh well, problems arise no matter what language you use. There's probably a perfectly reasonable explenation, otherwise it's a bug, but I really doubt that since I had this problem more than a year ago (never said anything about it because it was on a now abandoned project) and it would have been fixed if it's that easy to recreate. Do you have a link to that blog entry? Maybe I can see something that you do that I do as well to pinpoint the problem. Otherwise, the hack I use is quite simple and doesn't slow down anything. Would be nice to solve it in a clean way though.


    Quote Originally Posted by mr_malee
    I dont see anything wrong with that code.

    do you have warnings mode and strict mode on, maybe something is failing silently, like "this.stage" is undefined.
    Nope, this.stage is very much defined, and strict mode is on. It receives the events after I click on the swf. It's kind of like it looses focus, but it doesn't, because the deactivate event isn't triggered. My guess is something in my code is causing the focus to be set to something else, but I don't do this explicitely. Very strange.

    I'm probably too drunnk to post atm, it's 4:27 am and I have to get up in a coupl'a hours. Hope I made at least some sense. Bananas are not people, but both can be eaten.

  7. #7
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    The blog is linked in my sig, and the entry is "Kentish town smokers", although there's not a lot more info in there about the issue to be honest, just general rants.
    The blog contains the odd rude word, just a warning to younger members.

    Squize.

  8. #8
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Yeah already found it meanwhile, not much more info indeed.

    I just noticed another prob, which should also occur with the senocular class you use (mine is very similar, but I wrote it before he posted his class way back when). When you right click, the deactivate event isn't called, so keys can still get stuck. I'm having trouble finding a on-contextmenu event, but it's probably in there somewhere. Any ideas would be appreciated.

  9. #9
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Joy. I wasn't aware of that, thanks for flagging it up.

    If I have any luck finding a work around I'll post back here.

    Squize.

  10. #10
    Junior Member
    Join Date
    Jul 2008
    Posts
    1
    Thank you very much for this. I'm new to flash development, mixing and matching code and tips and whatnot from all over the place. I have something very similar to the Key class above and I was losing key events every time I switched out display objects.

    putting "stage.focus=stage" in my switching code worked like a charm (so far, anyway ).

  11. #11
    Junior Member
    Join Date
    Dec 2008
    Posts
    1

    Unhappy keyboard events on working here as well...

    I just ran into a similar issue yesterday, and would love to know if anyone has figured out what is going on?

    Is this a bug that may not be in CS4 players?

    i'm trying to put "stage.focus=stage" in various places to see if it will keep things going. I'm not sure i want it to be done every frame, but...

    the description "seems to randomly stops receiving key events...have to click on the [interface] again so it receives events again, so it seems to have lost focus, but there is no reason for that." fits very very well...

    It seems to only happen after I click on an item in the interface i'm building that then ends up getting swapped out with a different MovieClip, but at this point none of my MovieClips have any Key listeners in them, there is only one in the Main Class attached to the Stage....

  12. #12
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149
    i use stage.focus = this
    on the game class that handles the key input stuff.

    I have also run into problems though in the past where certain keyboards can only "hear" a certain number of key presses at the same time which might also be a problem....

    The CASALib stuff has some nice stage management as well that Im starting to play with, though not much of an opinion on it yet.
    Last edited by Flashkit; 12-13-2008 at 01:27 AM.
    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  13. #13
    Junior Member
    Join Date
    Nov 2004
    Location
    México
    Posts
    8

    Lightbulb I think I found the reason to lose focus

    Well, I had the same problem, so found this thread and the posts gave me the idea, it seems that when you click on any button, movieclip with mouse listener or input text fields, you lose focus in the stage which is the one listening for keyboard events, so, my solution was adding in every button function the line you gave before:

    stage.focus=stage;

    in my opinion it's not a player or code bug, it's the AS3 logic in which every element, even the stage is an object, so if you use another one, even being inside it, it loses focus, well, hope it helps, worked for me.

  14. #14
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    Quote Originally Posted by id_huevo View Post
    Well, I had the same problem, so found this thread and the posts gave me the idea, it seems that when you click on any button, movieclip with mouse listener or input text fields, you lose focus in the stage which is the one listening for keyboard events, so, my solution was adding in every button function the line you gave before:

    stage.focus=stage;

    in my opinion it's not a player or code bug, it's the AS3 logic in which every element, even the stage is an object, so if you use another one, even being inside it, it loses focus, well, hope it helps, worked for me.
    Yep, I have had to do that to.

  15. #15
    Senior Member Sietjp's Avatar
    Join Date
    Jan 2005
    Location
    Paris, France
    Posts
    711
    Do we have to add "stage.focus=stage;" on EVERY frame to turn around this bug ?

  16. #16
    Junior Member
    Join Date
    Nov 2004
    Location
    México
    Posts
    8
    nope, just in the button functions, so, for example

    myButton.addEventListener(MouseEvent.CLICK, stuffToDo);
    function stuffToDo (event_object:MouseEvent):void{
    // all of the button actions, and then:
    stage.focus=stage;
    }

    that's it, just put this line in any button or function that forces to lose focus from the main stage and you are all set, keyboard listeners will keep working, good luck

  17. #17
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Got the same problem, just find why. Focus on a object (sprite, mc, whatever) that you remove from stage and got garbage collect. Boom, loose stage focus.

  18. #18
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Actually, didn't even required garbage collection, just child removal of a (sprite / mc / whatever) with focus kill stage focus.

  19. #19
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    So, with this new information (which seems consistent with the other posts) it seems like we could add the
    stage.focus=stage;
    wherever we use removeChild() as well.

    Would it make sense to make a custom class called MySprite or something that inherits sprite and has a method myRemoveChild(args) that calls removeChild() along with the stage.focus=stage; line? Or would that just be silly?

  20. #20
    Member
    Join Date
    Mar 2007
    Posts
    73
    Thanks -

    Ran into the same problem. whatever.stage.focus = whatever.stage; worked great.

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