views:

2585

answers:

4

I have an interface that needs to react to "long key presses". That means 2 different actions for the same key based on how long the key has been down:

PRESS LEFT CURSOR: action A

PRESS & HOLD LEFT CURSOR: action B

Well, it's proving harder that I thought. The main problem is that both Keyboard.KEY-DOWN and Keyboard.KEY-UP fire continuously if you hold the key down. This looks weird to me although might make sense if you think about imputing text (if you are entering text and hold down a key it starts to fill the space).

When I was trying to tackle the problem I thought about starting a counter on the KEY-DOWN and resetting it on KEY-UP. Then if it reached certain threshold fire an event. The problem is the counter resets straight away because they KEY-UP event fires all the time.

I'm aware of the keyboard polling classes such as BigRoom's and Senocular's but they don't solve the problem because they base their status on the same events.

I could go for timers starting a timer on KEY-DOWN and after x milliseconds check if the key is still down but that would assume that the key has been down all the time. I don't think it would be reliable.

And this is the point I'm starting to run out of ideas. Is there a much simpler method I've overlooked? I hope there is!

Thanks,

Juan

A: 

Are you sure KEY_UP fires when you press a key down. That sounds sooooooo weird! My Flex trial has expired and I haven't set up an environment where I can compile using only the SDK so I can't experiment with this. But this actionscript.org forum thread about missing KEY_UP:s seems to suggest it could be a settings issue with the flash player. It's not the same problem as you're experiencing, but they would sure be complaining if they had your problem it seems...

PEZ
A: 

This http://www.futtock.co.uk/flash/smooth-movieclip-key-control-in-as3/ posting might contain a hint.

BTW: does your system generate a new key_down right after the unwanted key_up? In that case your keyboard could be in some weird setting making it auto-repeat a key that is being held down. Just a guess...

Simon Groenewolt
+1  A: 

Just wondering if you ever found a solution to this one. I hadn't had the same experience, so I went ahead and threw together a quick test to see whether I got the same result, and I didn't -- I only get the KEY_UP events when I actually let go of the key:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="app_applicationComplete()">

    <mx:Script>
     <![CDATA[

      private function app_applicationComplete():void
      {
       stage.addEventListener(KeyboardEvent.KEY_DOWN, app_keyDown);
       stage.addEventListener(KeyboardEvent.KEY_UP, app_keyUp);

       setFocus(); 
      }  

      private function app_keyDown(event:KeyboardEvent):void
      {
       trace("Down...");
      }

      private function app_keyUp(event:KeyboardEvent):void
      {
       trace("UP!");
      }

     ]]>
    </mx:Script>

</mx:Application>

My results look more like this -- three taps:

Down...
UP!
Down...
UP!
Down...
UP!

... and a "hold down and release":

Down...
Down...
Down...
Down...
Down...
Down...
Down...
Down...
Down...
Down...
Down...
Down...
UP!

Just figured I'd share; I guess it's also possible, though unlikely, your keyboard's sending "up" messages to the OS when it shouldn't be. But assuming you can make this work, you could probably just count through the number of successive KEY_DOWN notifications, resetting the counter when you get a KEY_UP, to infer the key's being held down rather than tapped and let go. Hope it helps!

Christian Nunciato
Hi there,"your keyboard's sending "up" messages to the OS when it shouldn't be"This put me in the road to find out the problem. Ta : )
Zárate
Great, I was wondering how this one turned out. Let me know how it goes!
Christian Nunciato
+1  A: 

Hi everybody.

Sorry that took me so long to come back, but was on the middle of the project and really didn't have time to look deeper into this. Now I have and I've found that there's no such problem. Read below for the long answer.

This is only hapenning under very specific circumstances and probably none to blame Adobe.

I have an iMac running Ubuntu and develop Flash on a virtual machine running Windows XP. This problem only appears on the virtual XP, the same application running natively on Ubuntu (and probably other systems) doesn't show the problem.

And because I have some other keyboard quirks when using the VM, I think the problem is on VirtualBox's keyboard driver.

Thanks to everybody that took the time to look into this. I hope this helps for other people!

Juan

Zárate
Ah! This is the virtual machine doing something strange to emulate auto-repeat. Very interesting. (By which I mean, very strange.)
Curt Sampson