How to capture the arrow keys in node.js

前端 未结 5 1171
南旧
南旧 2020-12-29 05:57

What is the utf8 code for all four arrow keys (up down left right)?

I am learning node.js and I am trying to detect whenever these keys are being pressed.

He

5条回答
  •  醉酒成梦
    2020-12-29 06:44

    A mix of Moezalez and user568109 answers worked for me:

    var stdin = process.stdin;
    // Don't set the rawMode to continue listening for other full length options.
    stdin.resume();
    stdin.setEncoding('utf8');
    
    stdin.on('data', function(input) {
        // Take out the new line character at the end
        var option = input.substr(0, input.length - 1);
        switch (option) {
            case '\u001b[A':
                // Up
                break;
            case '\u001b[B':
                // Down
                break;
            case '\u001b[C':
                // Right
                break;
            case '\u001b[D':
                // Left
                break;
            case 'something_else':
                // Perform what something_else does 
                break;
        }
    });
    

提交回复
热议问题