# Using onKeyDown within a class to call a function

**URL:** <https://forum.kirupa.com/t/using-onkeydown-within-a-class-to-call-a-function/217047>\
**Category:** Uncategorized\
**Created:** [February 22, 2007, 4:19pm UTC](https://forum.kirupa.com/t/using-onkeydown-within-a-class-to-call-a-function/217047 "2007-02-22T16:19:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luftbuefel](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@luftbuefel](https://forum.kirupa.com/u/luftbuefel)\
**Post date:** [February 22, 2007, 4:19pm UTC](https://forum.kirupa.com/t/using-onkeydown-within-a-class-to-call-a-function/217047/1 "2007-02-22T16:19:18Z")

</div>

Hello, I’m having a bit of trouble with some of the functions in a class I am making. I want to see if a button was pressed, then change some variables, and finally call the update function. My problem is that I cannot call the function updateit() once a key has been pressed. Any help would be greatly appreciated. Here’s the simplified code:

public function updateit():Void{  
trace(“updateit worked”);  
}

public function navigate():Void {  
var myListener = new Object();  
myListener.onKeyDown = function() {  
if (Key.isDown(Key.RIGHT)) {  
//change some variables  
trace(“right”);  
updateit();  
}  
if (Key.isDown(Key.LEFT)) {  
//change some variables  
trace(“left”);  
updateit();  
}  
if (Key.isDown(Key.UP)) {  
//change some variables  
trace(“up”);  
updateit();  
}  
if (Key.isDown(Key.DOWN)) {  
//change some variables  
trace(“down”);  
updateit();  
}  
};  
Key.addListener(myListener);  
}

Thanks for taking a look at it for me.
