'How to detect a keypress event inside a function? Nodejs

How can a keypress be detected only inside a function and not on all code? I have this code:

async function sellFunction() {

    let i = 0;
    let handle = setInterval(function() {
        logUpdate("Hello" + i++)
    }, 100);
    gkm.events.on('key.*', async(data) => {
        if (gkm.events.event === 'key.released' && (data[0] == 'A' || data[0] == 'B' || data[0] == 'C' || data[0] == 'D' || data[0] == 'F')) {
            clearInterval(handle);
            gkm.events.removeAllListeners();
            await init();
        }
    });

}

The problem with this is that this key event is being detected even if im in another function.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source