PDA

View Full Version : key press



Shadowwave
10-16-2006, 07:07 PM
hi, i was wondering how i can do a command like this:

on key press "s"
_root.~movie clips~ gotoAndPlay "framename1"

on key release "s"
_root.~movieclips~ gotoAndPlay "framename2"

i stopped using flash for a few years and i forget the commands for this, can someone show me please :rolleyes:

Black
11-01-2006, 08:15 PM
I wish there was an easy way to explain how the program could recognise the keys pressed from the key board...

there IS actually a "onKeyDown" event handler... but you would have to do a series of preperations related to the the movie you had wished to have played.

One of many ways would be to create what is called a "Listener". This would give you acess to a wider range of options.

So in order to first introduce the listener object declare;
keyListener=new Object ()

then asighn the event to the Key and include a function;
keyListener.onKeyDown=functio() {k=Key.get Ascii()}
to figure out what key was pressed.

Each key has a numerical return for every key on the key board.
You may want to trace out the particular key presed out, ending up with a script sort of like this;
keyListener = new Object();
keyListener.onKeyDown = function() {
k = Key.getAscii();
trace(k);
};

This would sort of lock your listener, so include a:
key.addListener(keyListener);
to the end of your function above (AFTER it).


Had you wished to have the key you listened for be something like the arrow keys or the shift or the space bar... it would have been a lot more easier for your part with key "Property" constants such as "keyLEFT" or "keySHIFT"...
but in usage of the rest of the keyboard you will have to use the associated Ascii values.
You can find a list of them HERE (http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00000963.html)

The Ascii value and the codes were not exactly the same for letters pressed with Shift and without... but for the codes it shouldn't change. Thus regarldess to underscore or not, the code for "S" would be 83.
In this case you would have to use "key.getCODE()" instead of "key.getAscii" and have it "==" to 83

But if you wish to distinguish the capital "S" from small "s", you are going to have to use the forumla given above with the codes below:
S = 83
s = 115



Sooo to wrap it up for the letter "S" only

you would need to use a script sort of like this one to run a movie from a particular frame onwards:
keyListener = new Object();
keyListener.onKeyDown = function() {
if(Key.getCode()==83){
gotoAndPlay(Aliceinwonderland)
}
}
key.addListener(keyListener);

Now important note here is for you to replace the key.getCode with getAscii if you care for it to be a capital "S" or a small one.
Such as;
if(Key.getAscii()==83) for S
if(Key.getAscii()==115) for s


Aditionally... I only went for onKeyDown simply because of your post. But I personally believe onKeyUp (It triggers when the key is released) would work better for most cases of going to particular frames or loading external movies.


I strongly advise you to read through the events/listners/properties related to the "key" object... it seems that it really HAS been some while since you were into it.


I hope this was of some help.


Black

TwinsenDude
11-02-2006, 12:27 AM
on (Key.isDown(Key.LEFT)) {
gotoAndPlay("framename1")
}

Replace LEFT with either a generic key like that or with a specific key code. I think you can find the key codes in the actionscript dictionary or in flash's help.

Black
11-02-2006, 01:09 AM
Are you sure?!


"on" is a mouse event handler... shouldn't be able to asign it on keys.
And key.LEFT is a key "constant" object.. I am not sure if the codes would work with them in this case...

I'll have to test this out when I get to be on one of the computers that hs Flash installed.
But out of my head... I sort of doubt it'll work.

Incase you can, and would like to give it a try right away;
the code for "S" is 83



Black