Is it possible to read from the console with scan without echoing the characters?

大城市里の小女人 提交于 2019-12-07 03:54:07

问题


Here is an example function:

    passwordEntry <- function() {
            cat("Enter your password: ")
            pwd <- scan(n=1, what=character(), quiet=TRUE)
            invisible(pwd)
    }

And to test the function:

    >   passwordEntry()
    Enter your password: 
    1: test
    > 

Is there a way to suppress what the user types? Or replace it with some other character? I have written a tcl/tk function to prompt the user for a password but it doesn't work on our Linux server.

Here is an example from Paul's link below. This does not work either on Linux or Windows (the latter probably because I don't have a proper C compiler so will look into that).

getkey3.c

    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    void mygetch ( int *ch ) 
    {
            struct termios oldt, newt;
            tcgetattr ( STDIN_FILENO, &oldt );
            newt = oldt;
            newt.c_lflag &= ~( ICANON | ECHO );
            tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
            *ch = getchar();
            tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
            return;
    }

And my test.r script:

    system('R CMD SHLIB getkey3.c')
    dyn.load("getkey3.so")
    .C("mygetch",as.integer(0))
    dyn.unload("getkey3.so")

I get this:

    > .C("mygetch",as.integer(0))
    [[1]]
    [1] -1

回答1:


This post covers how to read individual keystrokes:

Detecting single keystrokes

The accepted answer uses a small piece of C code that returns individual keystrokes. You can then capture the keystrokes for your password and echo nothing to the user or maybe a *.



来源:https://stackoverflow.com/questions/12517986/is-it-possible-to-read-from-the-console-with-scan-without-echoing-the-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!