Skip to main content
arofa
Participant
Feb 27, 2018 4:58 AM UTC
Answered

AS3

Is it possible in AS3 to have a left click mouse function to drag and holding control key + click to do something else?

Thank you,

arofa

This topic is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Feb 27, 2018 10:48 AM UTC

Hi.

You can do something like this:

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.MouseEvent;

var ctrl:Boolean = false;

var mouseDown:Boolean = false;

function start():void

{

    stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);

    stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);

    stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

}

function mouseHandler(e:MouseEvent):void

{

    if (e.type == MouseEvent.MOUSE_DOWN)

        mouseDown = true;

    else if (e.type == MouseEvent.MOUSE_UP)

        mouseDown = false;

  

    if (e.type == MouseEvent.MOUSE_MOVE)

    {

        if (ctrl && mouseDown)

            trace("ctrl + dragging");

    }

}

function keyHandler(e:KeyboardEvent):void

{

    if (e.type == KeyboardEvent.KEY_DOWN)

    {

        if (e.keyCode == Keyboard.CONTROL)

            ctrl = true;

    }

    else if (e.type == KeyboardEvent.KEY_UP)

    {

        if (e.keyCode == Keyboard.CONTROL)

            ctrl = false;

    }

}

start();

I hope it helps.

Regards,

JC

4 replies

Colin Holgate
Inspiring
Feb 27, 2018 2:26 PM UTC

I'm not sure if JC's idea will work out, but there is already a context menu system in place, since Flash 9. Here's the documentation:

ContextMenu - Adobe ActionScript® 3 (AS3 ) API Reference

Translate
JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Feb 27, 2018 10:48 AM UTC

Hi.

You can do something like this:

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.MouseEvent;

var ctrl:Boolean = false;

var mouseDown:Boolean = false;

function start():void

{

    stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);

    stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);

    stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

}

function mouseHandler(e:MouseEvent):void

{

    if (e.type == MouseEvent.MOUSE_DOWN)

        mouseDown = true;

    else if (e.type == MouseEvent.MOUSE_UP)

        mouseDown = false;

  

    if (e.type == MouseEvent.MOUSE_MOVE)

    {

        if (ctrl && mouseDown)

            trace("ctrl + dragging");

    }

}

function keyHandler(e:KeyboardEvent):void

{

    if (e.type == KeyboardEvent.KEY_DOWN)

    {

        if (e.keyCode == Keyboard.CONTROL)

            ctrl = true;

    }

    else if (e.type == KeyboardEvent.KEY_UP)

    {

        if (e.keyCode == Keyboard.CONTROL)

            ctrl = false;

    }

}

start();

I hope it helps.

Regards,

JC

Translate
arofa
arofaAuthor
Participant
Feb 28, 2018 10:17 AM UTC

Thank you very much for your help João César, that is what I needed

Arofa

Translate
JoãoCésar17023019
Community Expert
Community Expert
Feb 28, 2018 12:05 PM UTC

Excellent!

You're welcome!

Translate