How to combine keyboard press with app event?

without pressed key Y, console.log(pos, name); works logging pos and name but
I want to combine keyboard press with app event how to do it?
so I can after key Y was pressed do sth in onTriggerData callback
I tried in init but then key press not working so trying in update

update(dt) {
        this.addPos(this.entity.getPosition().clone());


        const onTriggerData = function (pos, name)
        {

            console.log(pos, name);
            this.triggerPos = pos;
            console.log('key y pos', this.triggerPos);
        }.bind(this);

        if(this.app.keyboard.wasPressed(pc.KEY_Y)){
            console.log('y');
        }
        this.app.on('triggerData', onTriggerData);

    }

@grzesiekmq, I’d say just initialize a boolean variable to false in your initialize function. Then, if the key is pressed, change it to true. In your event function, check whether it is true. If it is true, do whatever you need to and then set it back to false.

You are adding an event listener to the app event every frame as this is the update function. I don’t think that’s what you want.

yes I moved the

const onTriggerData = function (pos, name)
        {

            console.log(pos, name);
            this.triggerPos = pos;
            console.log('key y pos', this.triggerPos);
        }.bind(this);

        
 this.app.on('triggerData', onTriggerData);

to initialize

What are you trying to do? Fire an event on the app object when the user presses a key?

and update
hmm firing the event on key Y no
just firing the event and when the key Y was pressed set pos Y of trigger to update it
but temp I just console.log the pos

update(dt) {
        this.addPos(this.entity.getPosition().clone());

        if(this.app.keyboard.wasPressed(pc.KEY_Y)){
            this.check = true;

            if(this.check){
                
                console.log('key y position', this.triggerPos);

                this.check = false;
            }

        }

    }

Hello @grzesiekmq! In your last post this.check is always true when you do the if statement. I don’t quite understand what your problem is yet. Can’t you just fire the event when the Y button was pressed? Or maybe only update the position when the Y button is pressed.