View Full Version : Using Shift and Left / Right Arrow as Naviagtion
adrian7474
June 25th, 2009, 01:53 PM
I found this tutorial on kirupa about identifying keyboard and using them to navigate, however I cant seem to get it to work using (shift) and left, right, up, down.
I am going to use left and right arrows for one thing and shift left or right to do another.
Does anyone know how to make this happen?
Here is my code so far (not working for combing shift and value)
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);
GrndMasterFlash
June 25th, 2009, 03:17 PM
16+38=54 :P
what you should is have a KEY_DOWN and KEY_UP listener, when the key goes down have it set a boolean var to true (and likewise up = false), then instead of (keyEvent.keyCode==(16+38)) use ((shifIsDown==true)&&(upIsDown==true))
ya get it?
adrian7474
June 25th, 2009, 06:01 PM
kinda of, not sure how to implement..I am newbish. Can you post code? OR fix mine.
--adrian :)
Sketchbookshark
June 25th, 2009, 08:33 PM
First of all you have to replace the plus with the correct expression for what you are trying to say to Flash, which is the so -called "logical-AND", written as "&&", rather then the mathematical expression "+" you used.
secondly there is actually a special KeyboardEvent class variable for what you need by the name of ctrlKey which is boolean (true or false), exactly what GrndMaster Flash suggested, only you don't have to built it yourself. So try the following code:
else if (keyEvent.keyCode == 16 && keyEvent.ctrlKey == true) {
trace("Shift and Up Arrow pressed.");
}
That should work.
adrian7474
June 26th, 2009, 11:39 AM
Yea, didn't work. :(
I tried this it with "ctrlKey" code you supplied and nothing happened. Then i changed it to ****Key (see below), however every time the shift key is pressed alone it traces the function, not when shift and up arrow is pressed.
//code below//
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 == 16 && keyEvent.shiftKey == true) {
trace("Shift and Up Arrow pressed.");
}
else if (keyEvent.keyCode == 37) {
trace("Left Arrow pressed.");
}
else if (keyEvent.keyCode == 39) {
trace("Right Arrow pressed.");
}
}
GrndMasterFlash
June 26th, 2009, 11:44 AM
(keyEvent.keyCode == 16 && keyEvent.shiftKey == true)
should be
((keyEvent.keyCode == 16) && (keyEvent.shiftKey == true))
Sketchbookshark
June 26th, 2009, 04:35 PM
This is crazy, I have tried everything suggested, even looked up O'Reilly's official solution. Nothing works. The following code has some tracers to help figure this out. If anyone wants to give it a shot.
When I registered the event-Listener with the stage Flash did not register ANY letter-keys, only numbers and non-letters like brackets , dashes and dollarsign,etc..
When I attached the EventListener to the textfield my cursors sometimes went away when I pressed ctrl, sometimes it did not. Very inconsistent. Anyways no other keyboard Event was registered while I held the Control-key down. Anybody know what's wrong and why not even the O'Reilly solution works?
:stunned:
var textfield:TextField = new TextField();
textfield.height=200;
textfield.width=500;
textfield.background=true;
textfield.backgroundColor=0x128753;
textfield.border=true;
textfield.type=TextFieldType.INPUT;
textfield.addEventListener(KeyboardEvent.KEY_DOWN, keysDown);
textfield.addEventListener(KeyboardEvent.KEY_UP, keysUP);
addChild(textfield);
var controlPressed:Boolean=false;
function keysDown(e:KeyboardEvent):void {
if (e.keyCode==17) {
controlPressed=true;
trace("ctrl-Key registered");
}
if (controlPressed==true) {
trace("keypress registered as ="+e.keyCode);
if (e.keyCode==83) {
trace("Bingo");
}
}
}
function keysUP(e:KeyboardEvent):void {
if (e.keyCode==17) {
controlPressed=false;
trace("ctrl release registered");
}
}
Sketchbookshark
June 26th, 2009, 04:47 PM
Ok, this seems to be a very complicated issue. There is actually a tutorial here on kirupa. Too long for me to read right know.
Go check it out adrian7474 (http://www.kirupa.com/forum/member.php?u=147509)
http://www.kirupa.com/developer/actionscript/multiple_key_detection.htm
still don't get why the example from O'reilly would not work
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.