是否可以通过扫描从控制台读取而不回显字符? [英] Is it possible to read from the console with scan without echoing the characters?
问题描述
这是一个示例函数:
passwordEntry <- function() {
cat("Enter your password: ")
pwd <- scan(n=1, what=character(), quiet=TRUE)
invisible(pwd)
}
并测试功能:
> passwordEntry()
Enter your password:
1: test
>
有没有一种方法可以禁止用户输入?还是用其他字符替换它?我已经编写了一个tcl/tk函数来提示用户输入密码,但是它在我们的Linux服务器上不起作用.
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.
以下是Paul链接中的一个示例.这在Linux或Windows上均不起作用(后者可能是因为我没有合适的C编译器,因此我们将对此进行研究).
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;
}
还有我的test.r
脚本:
system('R CMD SHLIB getkey3.c')
dyn.load("getkey3.so")
.C("mygetch",as.integer(0))
dyn.unload("getkey3.so")
我明白了:
> .C("mygetch",as.integer(0))
[[1]]
[1] -1
推荐答案
这篇文章介绍了如何读取各个按键:
This post covers how to read individual keystrokes:
可接受的答案使用一小段C代码返回单个击键.然后,您可以捕获密码的击键,并且不向用户或*
发出任何回声.
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 *
.
这篇关于是否可以通过扫描从控制台读取而不回显字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!