# Keyboard event for the ENTER key

**URL:** <https://forum.kirupa.com/t/keyboard-event-for-the-enter-key/305965>\
**Category:** flash\
**Created:** [March 6, 2010, 6:39pm UTC](https://forum.kirupa.com/t/keyboard-event-for-the-enter-key/305965 "2010-03-06T18:39:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [March 6, 2010, 6:39pm UTC](https://forum.kirupa.com/t/keyboard-event-for-the-enter-key/305965/1 "2010-03-06T18:39:50Z")

</div>

Hi,

I have a button on the stage called enterCostBtn that when it is pressed it executes the code inside the calculateCost function. What I want to do is to be able to use the ENTER key on my keyboard to trigger that function. In other words I would like to be able to use the ENTER key or click directly on the enterCostBtn to trigger this function.

```auto

enterCostBtn.addEventListener(MouseEvent.CLICK,calculateCost,false,0,true);

function calculateCost(event:MouseEvent):void {
	var material:Number=Number(Material_txt.text);
	var labor:Number=Number(Labor_txt.text);
	var percent:Number=Number(Percent_txt.text);
	var purchasePlusLabor=material+labor;
	var plusPercent=labor*percent/100;
	var totalCost=purchasePlusLabor+plusPercent;

	txtCost.text=String("$ "+totalCost);

	trace(purchasePlusLabor + " Purchase Plus Labor");
	trace(totalCost + " Total Cost");
}

```

I tried something like this but it doesn’t work.

```auto
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
function onKeyPressed(evt:KeyboardEvent):void {

	switch (evt.keyCode) {
		case Keyboard.ENTER :
			calculateCost();
			break;

	}
}

```

Can someone try to help me with this? As always thank you for your help!
