无法区分标准输入原始模式下的 up/down 箭头

Cannot distinguish between up/down arrow in stdin raw mode

我有这段代码:

  process.stdin.setRawMode(true).resume();

  process.stdin.on('data', (buf) => {

    const str = String(buf);
    const charAsAscii = String(buf.toString().charCodeAt(0));

    switch (charAsAscii) {

      case '25': // left arrow ?
        console.log('left arrow');
        return;

      case '26': // right arrow ?
        console.log('right arrow');
        return;

      case '27': // down arrow
        console.log('down arrow');
        return;

      case '28': // up arrow?
        console.log('up arrow');
        return;

      default:
        console.error('default')
    }

}

所有箭头键似乎都被识别为向上箭头,也就是说,所有 4 个箭头键始终匹配大小写 '28' ...我希望区分 up/down/left/right方向键,有人知道怎么做吗?

对于箭头键,缓冲区有点不同:有 3 个字节。 我不知道它们代表什么,但我只知道你必须获得第三个字符:

process.stdin.setRawMode(true)
process.stdin.on('data', (data) => {
    const str = data.toString();
    if (str.length == 3) {
        console.log(str.charCodeAt(2))
    }
})

箭头代码:

向上 -> 65
向下 -> 66
右 -> 67
左 -> 68