0

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.

skzao
  • 1
  • 3
  • 1
    All key listeners are global. What's your other function doing? – ggorlen Apr 06 '22 at 22:58
  • You should be able to remove your event listener when you don't want to listen to it anymore. I'm sure the gkm.events API has some sort of off() or removeListener() function. – Scotty Jamison Apr 06 '22 at 23:00
  • I'm using itt look at the code ahah. – skzao Apr 07 '22 at 15:20
  • I'm not sure about the gkm.events... API, but when using the process.stdin.setRawMode(true), then you can use process.stdin.once() to only process a single key press event. Not sure if that helps, but it helped me. Also not sure if that would be an answer. But it might help guide you toward an answer. – Seth Eden Mar 07 '23 at 23:43

0 Answers0