Skip to main content
August 15, 2008
Question

dynamic keyboard-event

  • August 15, 2008
  • 5 replies
  • 968 views
hello!

i want to make some dynamic buttons which are able to use by pressing a letter and klick them. the problem is, that i try do test it by using trace() and so on but there is no feedback by the flesh when i pressed something. does anybody know what's wrong?

the event looks like that:

private function mausMarkiert(n)
{
n.currentTarget.alpha = 1;
// AENDERN --- hier noch die möglichkeit des tastendrückens und exemplare verschiebens einbauen
container_kalkulator_variante.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
}

private function mausUnmarkiert(n)
{
n.currentTarget.alpha = .8;
}

the alpha-manipulation works. do i have to write something more? container_kalkulator_variante is a sprite.

thanks for reading, understanding and re-writing!
This topic has been closed for replies.

5 replies

August 16, 2008
Sprites don't respond well to the keyboard event, as I mentioned before, you'll have to add the keyboard event listener to the stage, that's pretty much the only option. Sure things will trace everytime you press a key, so you'll need to code it so that actions happen when a key is down and an object is focused. Try this:

import flash.display.*

private var keyIsDown;

private function mausMarkiert(n)
{
n.currentTarget.alpha = 1;
stage.addEventListener(KeyboardEvent.KEY_DOWN,reportKey);
}

private function reportKey(e:KeyboardEvent){
trace(e.keyCode);
keyIsDown = e.keyCode;
}

After you load flash and click on the stage, it will tell you what key is pressed. After this, you'll need to add code that listens for a Focus Event.

myButton.addEventListener(FocusEvent.FOCUS_IN,doSomething);

private function doSomething(e:FocusEvent){
if(e.target == myButton){
if(keyIsDown == keyIwant){
//do something.
}
}
}

Cheers,
FlashTastic
August 16, 2008
so well. i've try it and it doesn't work and that make's me sad.

so i will explain what's on my monitor and what's happaning and what i want to do.
1) monitor
i've a container (a for example) with an container (b) and a container (c). container (c) is my butten, and in it i have a big sqare and some text.
2) what's happaning
++ when flash loaded everything and i move my mouse around whithaut pressing it anywhere and after this i press a letter ... nothing
++ when i pressed the mouse on anywhere exept (c) and after this a letter ... nothing
++ when i pressed the mouse in (c), but not the text, and after this a letter ... nothing (and this nothing is bad)
++ when i pressed the mouse in the text on (c) and after this a letter ... i get a trace with the letter and it's character code (like in the "f1"-help-example) --- but when i pressed with the courser anything else after this eccept the text in (c) and use my keyboard, again nothing happaned.
++++ this is the effect when i type

b.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);

in a function for my "mouse over (c)" and it's also the effect when i put this line after the first lines like

public class ...
{
public function ...
{
(right here)
}
}

the hole other buttom-stuff like manipulating alpha etc is working - so the funcions and events are working like they should. for me it seems like the is the need of something like "fokus" on my (c)-container to become a reaktion for the keypressevent, but i don't know if this is right and when how to do something like that.
3) the final user should be able to, for example, put 5 cookies from the first butten (c) to an other button (c') by drag ond drop (c) over (c') by holding the keyboard-ding "5". so, for this i think i need a mouse over (so that flash knows where the cookies come from), a keyevent and after this a mousemoouveevent.

until now the user is not able to use the keyboard because i have no code for it.

thank's for reading and for any, maybe, help
August 15, 2008
Oh, sorry. I didn't notice your code was in an external class. You'll have to import the stage as well.

import flash.display.Stage;


Cheers,
FlashTastic
August 15, 2008
ah haa. nun, my computer-akku is empty, so i have to wait until i'm home and off the net and on the energy to use the tip. but thank's for helping all the time.
August 15, 2008
You need to add the event listener to the stage instead of the sprite

stage.addEventListener(KeyboardEvent.KEY_DOWN,reportKeyDown);

function reportKeyDown(e:KeyboardEvent){
switch(e.keyCode){
case Keyboard.DOWN: //sprite do something;
break;
case Keyboard.UP: //sprite do something;
break;
default: //default action
}
}

Cheers,
Flashtastic
August 15, 2008
tja, thank you, but when i used your code (stage.add...), i get a 1009 - error. hmm...
Participant
August 15, 2008
You have added an EventListener that is calling the reportKeyDown function but I see no reportKeyDown() defined. You would also need:

function reportKeyDown(event:Event):void {
trace('entered reportKeyDown');
}

Hope that helps,
Dan
August 15, 2008
ah, well, thank you, i've forgotten to copy it on my post. so, i have it allready down under in my code - the same code as it is in de "F1" help to find. this is not the problem. i thought the problem is that i have to tell my spite that it is a part of my keyboard-activities bevor i can use some event-handler, but i don't know how.