AS3 "a" key press problems
- Halosheep
-
Halosheep
- Member since: Apr. 9, 2009
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
Hi I'm trying to make a system where i can just loop through to check all the keys instead of coding in each one manually, and everything seems to work but the "a" key. I can press it once, and it will register, but any subsequent presses have no effect at all. It doesn't matter when I press it, and all the other keys work fine, it's just the "a" key will only register once. If anyone could just test this on another computer to check if my keyboard is broken or spot any bugs in the code I would greatly appreciate it.
import flash.events.KeyboardEvent;
var inputKeys:Array = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z");
function inputKeyDown(event:KeyboardEvent):void
{
var keyI:int = 0;
for (var i:int = 0; i < 26; i ++)
{
keyI = 65 + i;
if (event.keyCode == keyI)
{
trace(inputKeys[i]);
}
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, inputKeyDown);
function inputKeyUp(event:KeyboardEvent):void
{
event.keyCode = 0;
trace("NO KEY");
}
stage.addEventListener(KeyboardEvent.KEY_UP, inputKeyUp);
For right now the alphabet array is just there to print out stuff, it will later be a boolean array that I can check in the main code to do things like moving or whatever. The intputKeyUp function doesn't actually do anything, I was just checking to see if resetting the keyCode after every key press would solve the problem, but it didn't.
Here's some sample output
s
NO KEY
d
NO KEY
f
NO KEY
d
NO KEY
s
NO KEY
a
NO KEY
//Me furiously hitting the "a" key again, nothing happening
d
NO KEY
Thanks in advance
"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes
- Redshift
-
Redshift
- Member since: Feb. 12, 2005
- Offline.
-
- Forum Stats
- Member
- Level 15
- Programmer
Did you try disabling keyboard shortcuts?
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
- Nogostradamus
-
Nogostradamus
- Member since: Feb. 22, 2010
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
import flash.events.KeyboardEvent;
var inputKeys:Array = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z");
stage.addEventListener(KeyboardEvent.KEY _DOWN, inputKeyDown);
stage.addEventListener(KeyboardEvent.KEY _UP, inputKeyUp);
function inputKeyDown(event:KeyboardEvent):void
{
var currentKey:uint = 65;
for (var i:int = 0; i < inputKeys.length; i ++)
{
currentKey += i;
if (event.keyCode == currentKey )
{
trace(inputKeys[i]); }
}
}
function inputKeyUp(event:KeyboardEvent):void
{
trace("NO KEY");
}
Maybe something like this will help
Game programming: RPG in AS3 && Graphic Design: TrueSign
- Redshift
-
Redshift
- Member since: Feb. 12, 2005
- Offline.
-
- Forum Stats
- Member
- Level 15
- Programmer
At 8/24/11 05:57 AM, Nogostradamus wrote: for (var i:int = 0; i < inputKeys.length; i ++)
{
currentKey += i;
if (event.keyCode == currentKey )
{
trace(inputKeys[i]); }
}
}
Failure. currentKey would skip around with that code.
65, 66, 68, 71, 75, 80, 86, ect...
The original code works just fine on my end. Not only does the original code not need correcting, you made it dysfunctional.
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
- Halosheep
-
Halosheep
- Member since: Apr. 9, 2009
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
Yeah I'm kind of stupid, after you said keyboard shortcuts I added a rectangle which changed colour when you pressed a key, and then ran that on the flash player, it always worked, just the shortcut got in the way somehow.
Lolz
Anyways, thank you for the replies and for helping me fix it
"If everyone did things 75% right people would be ecstatic, so I demand that you change this C to an A+!" - Calvin and Hobbes

