# Detect a key in an input field

**URL:** <https://forum.kirupa.com/t/detect-a-key-in-an-input-field/68487>\
**Category:** Uncategorized\
**Created:** [October 26, 2004, 10:49am UTC](https://forum.kirupa.com/t/detect-a-key-in-an-input-field/68487 "2004-10-26T10:49:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![skjc](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@skjc](https://forum.kirupa.com/u/skjc)\
**Post date:** [October 26, 2004, 10:49am UTC](https://forum.kirupa.com/t/detect-a-key-in-an-input-field/68487/1 "2004-10-26T10:49:20Z")

</div>

I have an input text field (myText\_txt) and I want to detect whether a certain key has been pressed and if so I take an action. I have two lots of code so far but neither works. Any ideas?

// in this one I set up a listener and apply it to the myText\_txt

myListener = new Object();  
myListener.onKeyDown = function() {  
if(Key.isDown(Key.SPACE)) {  
trace(“space bar”);  
}  
}  
myText\_txt.addListener(myListener);

// in this one I apply the onChanged to the myText\_txt

myText\_txt.onChanged = function () {  
if (key.isDown(Key.SPACE)) {  
trace(“space bar”);  
}  
}

neither of them detect the space bar key press. Is it possible to do this to an input field ??

I only want to pick up the key press within the text field.

thanks
