Groups | Blog | Home
all groups > flash actionscript > january 2007 >

flash actionscript : Are Key Listener combinations possible?


Angyl
1/7/2007 7:40:31 PM
How do I get a key listener to work with a combination of keys (like Shift+M)
rather than a single key? I'm using the keyCode method currently but with the
project I'm working on that forces me to map out a lot of the keyboard and gets
kind of confusing.

A snippet of my code is this:

keyListener1.onKeyDown = function() {
if (Key.isDown(77)) {
carrier1.carrierJet.gotoAndPlay(2);
delete this.onKeyDown
}

That works if I press M. How would I code it to work only if I press SHIFT+M?
David Stiller
1/8/2007 9:16:17 AM
Angyl,

[quoted text, click to view]

Aha. Well, the answer may come as a surprise, but only because it's so
obvious, once you see it. :)

[quoted text, click to view]

Here, you're listening for the M key, represented internally by the
number 77. You want to listen for the Shift key *in addition* to the M key,
so you'll need to use the logical AND operator in your if() statement.
Shift is key code 16 -- you can find them all under "Keyboard Keys and Key
Code Values" in the ActionScript 2.0 Language Reference" -- so this would be
your if() statement ...

if (Key.isDown(77) && Key.isDown(16)) { ... }

Make sense?


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

ggshow
1/9/2007 7:14:35 AM
David Stiller >
this code
if (Key.isDown(77) && Key.isDown(16)) { ... }
works for Shift+M, and works for M+Shift as well

how about this one:

var myListener:Object = new Object();
myListener.onKeyDown = function() {
if (Key.isDown(77)) {
if (_root.key == "Shift") {
carrier1.carrierJet.gotoAndPlay(2);
delete this.onKeyDown;
}
} else {
if (Key.isDown(16)) {
_root.key = "Shift";
}
}
};
myListener.onKeyUp = function() {
_root.key = "";
};
Key.addListener(myListener);

David Stiller
1/9/2007 8:14:18 AM
ggshow,

[quoted text, click to view]

That's right.

[quoted text, click to view]

Checking for M, so far.

[quoted text, click to view]

Now checking the value of a variable in the main timeline. If the value
of this variable is the string "Shift", then ...

[quoted text, click to view]

.... a movie clip by the instance name carrierJet is told to go to frame 2
and play. This carrierJet is nested inside another movie clip with the
instance name carrier1 (or carrier1 is some other object with a property
that points to carrierJet).

If if above is not true ...

[quoted text, click to view]

.... then *if* one of the shift keys is pressed, set that root variable to
the string "Shift".

Well ... to be honest, I'm not sure what that key variable is for. If I
understood right, Angyl wanted to test for the condition of Shift and the
key M at the same time. If so, the simplest route is to simply check for
both those keys. I'm not sure why a variable help matters, but maybe you
could explain?


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

ggshow
1/10/2007 7:01:35 AM
just trying to test either user press the Shift key 1st...:smile;

should have some other better ways to do that..

or... if in the application, "which key pressed 1st" is not important, then
your code is just perfect...
David Stiller
1/10/2007 9:40:04 AM
ggshow,

[quoted text, click to view]

Aha. Okay, that makes sense.

[quoted text, click to view]

Ehh, you work with what you have, right? That's one of the fun
challenges of programming: if the API doesn't offer it, roll your own. :)


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

AddThis Social Bookmark Button