We help IT Professionals succeed at work.

Arrow Keys as Navigation

850 Views
Last Modified: 2020-04-13
I am using arrows keys to navigate to certain frames in the timeline, however, I cannot figure out how to make the shift + arows to work. Any ideas?? Below is the code i am using.
Right now iam just using trace statements to test.
As you can see, i can't get shift+up to work.
function reactToArrowKeys(keyEvent:KeyboardEvent) {
 
 
 
if (keyEvent.keyCode == 38) {
trace("Up Arrow pressed.");
}
 
else if (keyEvent.keyCode == 40) {
trace("Down Arrow pressed.");
} 
else if (keyEvent.keyCode == [16+38]) {
trace("Shift and Up Arrow pressed.");
} 
else if (keyEvent.keyCode == 37) {
trace("Left Arrow pressed.");
} 
else if (keyEvent.keyCode == 39) {
trace("Right Arrow pressed.");
}
 
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, reactToArrowKeys);

Open in new window

Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi adrian7474,

adding the two values doesn't make sense, since the sum will just mean another key.

I have no experience with action-script (and I don't know why you posted this in MFC zone), but in internet (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/KeyboardEvent.html) I found that a 'KeyboardEvent' object has a property 'shiftKey' which indicates the SHIFT's state.

Hope that helps,

ZOPPO
CERTIFIED EXPERT

Commented:
you need to check for two key hits in one statement like this...

if ( (keyEvent.keyCode == 16) && (keyEvent.keyCode == 38) )

you will need to confirm that these are the actual keycodes that you get when you hit those keys though....


rp
 
CERTIFIED EXPERT

Commented:
hm - as told I has absoluteley no experience with action script, but I doubt that the 'keyEvent.keyCode' can have two different values at the same time ...
CERTIFIED EXPERT

Commented:
lol... um yeah, you are right... i was focusing on the structure of the conditional statement and not the syntax... my mistake.

but basically you need to compare two conditions in one if statement by using the double ampersand...

as for detecting if a shift and arrow are pressed together, you need to use something a bit different
so here is some proper code to use...

if( (Key.isDown(Key.SHIFT)) && (keyEvent.keyCode == 38) )


that should work for you...


rp



You need to call the function

   GetAsyncKeyState(VK_SHIFT);

to find out whether the shift was pressed or not.

Author

Commented:
No, this didn't help me. I am new to as3 and flash. I don't understand how to implement it.
CERTIFIED EXPERT

Commented:
wrong programming language itsme...

looking for Actionscript 2.0

;)

rp
So your function should be changed to somewhat like the below

VK_SHIFT equals to 0x10 or decimal 16 in case the VK_ constants are not available in AS3.

The GetAsyncKeyState returns a SHORT what is a 16bit integer. If the highest bit of that return value is set, the key is down. That's why I checked for

    GetAsyncKeyState(16) < 0

cause if the highest bit of an integer was set the number was negative.

But the above could go wrong if the AS3 turns it somehow to a 32bit integer. So in C/C++ it would be correctly to have

  if ((GetAsyncKeyState(16) & 0x00008000) != 0)

which explicitly would be test for bit 15 of the return value (what is the highest bit of a SHORT)


function reactToArrowKeys(keyEvent:KeyboardEvent) 
{
 
if (keyEvent.keyCode == 38) {
trace("Up Arrow pressed.");
}
else if (keyEvent.keyCode == 40) {
trace("Down Arrow pressed.");
} 
else if (keyEvent.keyCode == [16] && GetAsyncKeyState(16) < 0 ) {
trace("Shift and Up Arrow pressed.");
} 
else if (keyEvent.keyCode == 37) {
trace("Left Arrow pressed.");
} 
else if (keyEvent.keyCode == 39) {
trace("Right Arrow pressed.");
}
 
}

Open in new window

>>>> wrong programming language itsme...
I hoped they can use plain WINAPI ;-)
>>>> wrong programming language itsme...
And isn't MFC the first TA?
CERTIFIED EXPERT

Commented:
well, just because it is listed in the TA, doesn't make it right...  ya know  ;)

the code is Actionscript 3.0...  

and this gives it away...

stage.addEventListener(KeyboardEvent.KEY_DOWN, reactToArrowKeys);

that, and the fact that the Author Generated Tags say AS3

:)

rp

Author

Commented:
Hey Guys, thanks for the help, I feel we are close, but not quite there.
--I am sorry if I posted this in the wrong section, but I am new to this site :(

--Anyway, I get the following errors when i post "itsmeandnobodyelse" code :
1176: Comparison between a value with static type uint and a possibly unrelated type Array.
CERTIFIED EXPERT

Commented:
it is because you should not try to use C++ code in Flash...  for some reason Flash doesn't like that  :)

just replace the one conditional you have up top with the last one I gave you...


rp

Author

Commented:
Hey Rascal, I tried both your posts, and none worked, i got errors too, something like can't compare two statements or something.. :(


CERTIFIED EXPERT

Commented:
post the code you tried to implement

and the error as well...

rp
CERTIFIED EXPERT

Commented:
actually I just noticed that the Key class is no longer in AS 3....  

and I am not sure how to compare two codes where the events get fired at different times...

maybe set some variables that say the key was clicked and then do your conditionals based on that...


is that clear?

rp

CERTIFIED EXPERT

Commented:
Well, if the link I posted refers the language you use then IMO it should be like this:

else if (keyEvent.keyCode == [38] && keyEvent.shiftKey != false ) {
trace("Shift and Up Arrow pressed.");
}

But who knows ;o) ....

ZOPPO
CERTIFIED EXPERT

Commented:
remove the [ ] around the 38 and you basically got it...

what I mentioned above would work as well, but it seems this is closer to the Authors original script.


rp
CERTIFIED EXPERT

Commented:
sorry about the [] ...
CERTIFIED EXPERT

Commented:
yeah, the [ ], is what was causing the above comparison error, because hard brackets are interpreted as an array.

but other than that, the comparison should work fine.

rp

Author

Commented:
Sorry, still doesn't work...Did I not do something right?


stage.addEventListener(KeyboardEvent.KEY_DOWN, reactToArrowKeys2);


////shift test
function reactToArrowKeys2(keyEvent:KeyboardEvent)
{
 
if (keyEvent.keyCode == 38) {
trace("Up Arrow pressed.");
}
else if (keyEvent.keyCode == 40) {
trace("Down Arrow pressed.");
}
else if (keyEvent.keyCode == 38 && keyEvent.shiftKey != false ) {
trace("Shift and Up Arrow pressed.");
}
else if (keyEvent.keyCode == 37) {
trace("Left Arrow pressed.");
}
else if (keyEvent.keyCode == 39) {
trace("Right Arrow pressed.");
}
 
}

I can't believe this is that hard...urg flash kills me!
CERTIFIED EXPERT

Commented:
your if conditional needs to be seperated by putting your 2 conditions in parentheses... and you also don't need != false, because you are just checking a boolean property

try this...

else if ( (keyEvent.keyCode == 38) && (keyEvent.shiftKey) ) {



rp
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
good catch again zoppo...  I thought you had not actionscript experience  ;)

rp

Author

Commented:
Thanks so much, it finally worked..Yo da man.

Author

Commented:
Rascal, thanks for all your help too. :)
CERTIFIED EXPERT

Commented:
Well, I think with more than 20 years experience in C/C++ should be enough to solve problems like this one :o)

Have a nice day,

best regards,

ZOPPO
CERTIFIED EXPERT

Commented:
yeah, it is the same way I can read and understand Java, but have yet to compile an app  :)

It is not like Cobol or something completely backwards from normal programming theory...


rp