Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6112301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:43:48+00:00 2026-05-23T14:43:48+00:00

The keyBoard event listeners call on the rotate and unrotate functions and based on

  • 0

The keyBoard event listeners call on the rotate and unrotate functions and based on the key inputs(A and D) will implement either of the functions and rotate the image or return it to the original upright position. However, I haven’t been able to get the function to work. When I press the A or D keys on the keyboard nothing happens. I even put trace() in one of the functions to see if the function will even be implemented but I don’t get anything. I put in my Scripts below. Why are the functions not working? Do the eventlisteners fire or are there conflicts? I’m not getting any error messages.

ti.border = true
ti.addEventListener(TextEvent.TEXT_INPUT, onInput);
    function onInput(event:TextEvent):void {
 if(ti.text.search('a')!=-1) load_image("http://i54.tinypic.com/anom5d.png", "ottefct");
 else if(ti.text.search('b')!=-1) load_image("http://i53.tinypic.com/2dv7dao.png", "rnd");
 else if(ti.text.search('c')!=-1) load_image("http://i51.tinypic.com/m8jp7m.png", "ssd");
    }

var loaded_images:Dictionary = new Dictionary();

function load_image(url:String, id_name:String)
{
    var loader:Loader = new Loader();
    loader.name = id_name;
    var url_req:URLRequest = new URLRequest(url);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
    loader.load(url_req);
}

function onLoadingComplete(evt:Event):void
{
    var img_name:String = evt.currentTarget.loader.name
    var spr_box:Sprite = new Sprite();
    spr_box.addChild(evt.currentTarget.loader);

    spr_box.mouseChildren = false;
    spr_box.doubleClickEnabled = true;

    spr_box.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    spr_box.addEventListener(MouseEvent.MOUSE_UP, drop);
    spr_box.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
    spr_box.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);

    spr_box.width = 124;
    spr_box.height = 180;
    spr_box.x = 430;
    spr_box.y = 425;

    this.addChild(spr_box);
    loaded_images[img_name] = spr_box;
}


function drag(evt:MouseEvent):void
{
    evt.currentTarget.startDrag()
}

function drop(evt:MouseEvent):void
{
    evt.currentTarget.stopDrag()
}

function rotate(evt:KeyboardEvent):void
{
    if (evt.keyCode==68) {
    evt.currentTarget.rotation = 90 }
}

function unrotate(keyEvent:KeyboardEvent):void
{
    if (evt.keyCode==65) {
    evt.currentTarget.rotation = 0; 
    trace("A key pressed")}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:43:48+00:00Added an answer on May 23, 2026 at 2:43 pm

    Because sprites don’t necessarily dispatch keyboard events. Add the listener to the stage instead

    stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);
    

    You’ll need to keep track of which “spr_box” is selected, I’m guessing you’ll do that with a double click. Create a variable called currentSpr_box, and then in your listener function, set the currentSpr_box as the currentTarget

    spr_box.addEventListener(MoustEvent.DOUBLE_CLICK, onSelect); //add this when creating spr_box in onLoadingComplete
    
    private function onSelect(event:MouseEvent):void{
       currentSpr_box = event.currentTarget as Sprite;
    }
    

    In your rotate functions, you need do set currentSpr_box.rotation

    Update… full code sample

    ti.border = true
    ti.addEventListener(TextEvent.TEXT_INPUT, onInput);
        function onInput(event:TextEvent):void {
     if(ti.text.search('a')!=-1) load_image("http://i54.tinypic.com/anom5d.png", "ottefct");
     else if(ti.text.search('b')!=-1) load_image("http://i53.tinypic.com/2dv7dao.png", "rnd");
     else if(ti.text.search('c')!=-1) load_image("http://i51.tinypic.com/m8jp7m.png", "ssd");
        }
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);
    
    var loaded_images:Dictionary = new Dictionary();
    var currentSpr_Box:Sprite;
    
    function load_image(url:String, id_name:String)
    {
        var loader:Loader = new Loader();
        loader.name = id_name;
        var url_req:URLRequest = new URLRequest(url);
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
        loader.load(url_req);
    }
    
    function onLoadingComplete(evt:Event):void
    {
        var img_name:String = evt.currentTarget.loader.name
        var spr_box:Sprite = new Sprite();
        spr_box.addChild(evt.currentTarget.loader);
    
        spr_box.mouseChildren = false;
        spr_box.doubleClickEnabled = true;
    
        spr_box.addEventListener(MouseEvent.MOUSE_DOWN, drag);
        spr_box.addEventListener(MouseEvent.MOUSE_UP, drop);
        spr_box.addEventListener(MouseEvent.DOUBLE_CLICK, onSelect);
    
        spr_box.width = 124;
        spr_box.height = 180;
        spr_box.x = 430;
        spr_box.y = 425;
    
        this.addChild(spr_box);
        loaded_images[img_name] = spr_box;
    }
    
    function onSelect(evt:MouseEvent):void{
        currentSpr_box = event.currentTarget as Sprite;
    }
    
    function drag(evt:MouseEvent):void
    {
        evt.currentTarget.startDrag()
    }
    
    function drop(evt:MouseEvent):void
    {
        evt.currentTarget.stopDrag()
    }
    
    function rotate(evt:KeyboardEvent):void
    {
        if (currentSpr_box != null && evt.keyCode==68)  currentSpr_box.rotation = 90;
    }
    
    function unrotate(keyEvent:KeyboardEvent):void
    {
        if (currentSpr_box != null && evt.keyCode==65) currentSpr_box.rotation = 0; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I'm trying to just barely add keyboard event listeners in my program, then
I'm having trouble adding an event listener. I'm basically encapsulating all keyboard-related functions into
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Is it possible to have keyboard event listener canvas.addEventListener('onkeydown', ev_keydown, false); like we have
I am trying to forward a keyboard event from the Collection View to all
I'm trying to get familiar with the whole keyboard event detection thing. Here's my
I am using TextChanged event and when I am pressing keyboard, numbers are going
I currently use the onKeyDown event and an if/else statement to create keyboard shortcuts:
I'm doing a little project that involves the mouse and key listeners in JPanel.
It seems that it is impossible to capture the keyboard event normally used for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.