# Is there a way to handle events with two simultaneous keys (Ctrl + other key)?

**URL:** <https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413>\
**Category:** flash\
**Created:** [June 25, 2008, 10:00pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413 "2008-06-25T22:00:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![enb141](https://avatars.discourse-cdn.com/v4/letter/e/82dd89/32.png) [@enb141](https://forum.kirupa.com/u/enb141)\
**Post date:** [June 25, 2008, 10:00pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/1 "2008-06-25T22:00:05Z")

</div>

Hi I got this code

stage.addEventListener (KeyboardEvent.KEY\_DOWN, keyDownHandler);  
stage.addEventListener (KeyboardEvent.KEY\_UP, keyUpHandler);

function keyDownHandler (e:KeyboardEvent):void {

trace (  
"Key code: " + e.keyCode + "  
" +  
"Ctrl status: " + e.ctrlKey + "  
" +  
"Key location: " + e.keyLocation + "  
" +  
"Shift key: " + e.shiftKey + "  
");  
}

function keyUpHandler (e:KeyboardEvent):void {

trace ( "Key code: " + e.keyCode);  
}

and it works but now I need to use two simultaneous keys for example [COLOR=“Blue”]Ctrl + S[/COLOR], is there a way to do that using actionscript 3?

---

<div class="post-metadata">

**Author:** ![tpann](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tpann](https://forum.kirupa.com/u/tpann)\
**Post date:** [June 26, 2008, 2:16am UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/2 "2008-06-26T02:16:52Z")

</div>

Mine is probably not the most elegant approach, but I would do this by setting a boolean to true when CTRL is pressed, and then to false when it’s released. Then set up a function to execute when S is pressed, but that function will only execute if the CTRL boolean == true.

---

<div class="post-metadata">

**Author:** ![enb141](https://avatars.discourse-cdn.com/v4/letter/e/82dd89/32.png) [@enb141](https://forum.kirupa.com/u/enb141)\
**Post date:** [June 26, 2008, 5:54pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/3 "2008-06-26T17:54:36Z")

</div>

I did that but it doesn’t seems to work, look.

var ControlIsPressed:Boolean;

stage.addEventListener (KeyboardEvent.KEY\_DOWN, keyDownHandler);  
stage.addEventListener (KeyboardEvent.KEY\_UP, keyUpHandler);

function keyDownHandler (e:KeyboardEvent):void {

```
if (e.ctrlKey){
	ControlIsPressed = true;
}

if (ControlIsPressed){
	if (e.keyCode == 83) {
		trace("lalala");
	}
}

```

}

function keyUpHandler (e:KeyboardEvent):void {  
if (e.ctrlKey){  
ControlIsPressed = false;  
}

trace(“keyUpHandler  
” +  
"Key code: " + e.keyCode + “  
”) ;  
}

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 26, 2008, 5:59pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/4 "2008-06-26T17:59:19Z")

</div>

I’m by no means an expert on the topic, but can you really detect the ctrl key?

```auto

stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);

function keyUpHandler (e:KeyboardEvent):void {
	trace(e);
}

```

If I press ctrl, nothing traces. All other keys report an event.

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 26, 2008, 6:13pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/5 "2008-06-26T18:13:21Z")

</div>

You actually can trace the control key (it works for me with that exact same code) - but more importantly, the KeyboardEvent class includes a **ctrlKey** boolean.

Try this:

```auto

stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);

function keyUpHandler (e:KeyboardEvent):void 
{
    if (e.ctrlKey && e.keyCode == 83)
	{
		trace("Ctrl + S pushed.");
	}
	else
	{
		trace(e.keyCode)
	}
}

```

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 26, 2008, 6:28pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/6 "2008-06-26T18:28:47Z")

</div>

Not tracing anything on OSX : )

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 26, 2008, 6:32pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/7 "2008-06-26T18:32:19Z")

</div>

Hah. You know my feelings on OS X and Flash. :fight:

Maybe I should move the event listener one pixel to the left… :huh:

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 26, 2008, 6:34pm UTC](https://forum.kirupa.com/t/is-there-a-way-to-handle-events-with-two-simultaneous-keys-ctrl-other-key/264413/8 "2008-06-26T18:34:26Z")

</div>

try converting it to an int 😜
