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
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 *
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With