Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Detect enter in keypress event node.js rawmode

Detect enter in keypress event node.js rawmode

Problem

I am writing a simple commandline tool to allow commandline input to a node.js server. I am trying to make a buffer, so the user can press up and see the last command. To do that I have set

require('tty').setRawMode(true);

And detects all keypress with:

process.stdin.on('keypress', function (letter, key) {
    if (key && key.ctrl && key.name == 'c') {
        process.exit();
    } else if (key && key.enter) {
        write(letter);
        msgFired(buffer[bufferPos]);

        bufferPos += 1;
        buffer[bufferPos] = "";
    } else {
        write(letter);
        buffer[bufferPos] += letter;
    }
});

This does not detect enter-presses.

Are there a way to detect when a whole line is fires (as when RawMode is false) alongside with the keypress event? If not, how I detect the enter-press?

Problem courtesy of: JPuge

Solution

As far as I can see, your only error is that this:

else if (key && key.enter) {

should be this:

else if (key && key.name == 'enter') {
Solution courtesy of: loganfsmyth

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Detect enter in keypress event node.js rawmode

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×