Flash:AS3: Button click to trigger the right arrow key?
I'm trying to figure out a way to have a button basically trigger right arrow key when it is clicked. If someone could help me out with a code to do this I would 开发者_高级运维greatly appreciate it! Thanks in advace.
In general, this is a bad idea.
If you want the mouse button to perform the same operation as the key, you should refactor the code to a shared method:
function doSomething(){
// ...
}
function mouseClick(){
doSomething();
}
function keyPress(){
doSomething();
}
If you've set up your keystroke management similar to me then it should be easy..
I basically just have a class with an array which has a given index set to true/false based on the keyCode
of the pressed/released key..
A breakdown of this:
When I press "a" which has an ASCII or keyCode value of 65, then array[65]
is set to true
. When I release the "a" key, array[65]
is set to false
.
Then I just have a function that returns a boolean that represents a key being held down. Here's a quick example of this class:
package
{
import flash.display.Stage;
import flash.events.KeyboardEvent;
public class Keystrokes
{
public static var keys:Array = [];
public static function init(stg:Stage):void
{
stg.addEventListener(KeyboardEvent.KEY_DOWN, _keyDown);
stg.addEventListener(KeyboardEvent.KEY_UP, _keyUp);
}
private static function _keyDown(e:KeyboardEvent):void
{
keys[e.keyCode] = true;
}
private static function _keyUp(e:KeyboardEvent):void
{
delete keys[e.keyCode];
}
public static function keyIsDown(...ascii):Boolean
{
var i:uint;
for each(i in ascii)
{
if(keys[i]) return true;
}
return false;
}
}
}
Now with all that said you could basically do:
Keystrokes.keys[keycode_for_right_arrow] = true;
When you click the mouse, and false
when you release it.
精彩评论