跟踪用户是否输入了特定的"字"在WinForm上

时间:2015-04-19 07:04:13

标签: c# winforms

我有一个ScreenLocker winform应用程序。它是全屏和透明的。当用户按下Ctrl+Alt+Shift+P时,它会解锁屏幕。

但我希望它更有活力。我想让用户在配置文件中设置自己的密码。

例如,他设置了密码 mypass 。我的问题是 - 如何跟踪他是否在该表单上键入“mypass”?

我不想在表单上有任何文本框或按钮。请帮助。

这是我目前的代码 -

    public frmMain()
    {
        InitializeComponent();
        this.KeyPreview = true;

        this.WindowState = FormWindowState.Normal;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Bounds = Screen.PrimaryScreen.Bounds;
        this.ShowInTaskbar = false;
        double OpacityValue = 4.0 / 100;
        this.Opacity = OpacityValue;
    }

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.P && e.Modifiers == (Keys.Control | Keys.Shift | Keys.Alt))
        {
            this.Close();
        }
    }

4 个答案:

答案 0 :(得分:2)

您可以将输入的字母存储在变量中,然后在输入keydown事件时进行检查。这是工作样本 -

public partial class Form1 : Form
{

    String pass = String.Empty;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        string value = e.KeyChar.ToString();
        pass += value;
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode==Keys.Enter)
        {
           //Now check the password with your config file.
           //You may have to reset the variable if the pass does not match with the config.
        }
    }
}

答案 1 :(得分:2)

我认为最优雅和最强大的解决方案是使用反应式扩展框架。

PM> Install-Package Rx-Main 

按下的按键存储在缓冲的可观察序列中。当缓冲区与密码匹配时,将执行操作(在示例中,它是一个消息框)

private void Form1_Load(object sender, EventArgs e)
    {
        string password = "test";
        var keypressed = Observable.FromEventPattern<KeyPressEventHandler, KeyPressEventArgs>(
                handler => handler.Invoke,
                h => this.KeyPress += h,
                h => this.KeyPress -= h);


        var keyDownSequence = keypressed.Select(p => p.EventArgs.KeyChar);

        var checkPasswordSequence = from n in keyDownSequence.Buffer(password.Length, 1)
                                    where string.Join("", n.ToArray()) == password
                                    select n;

        checkPasswordSequence.Subscribe(x => MessageBox.Show(string.Join("", x.ToArray())));
    }

答案 2 :(得分:0)

这是有效的

public partial class LockScreen : Form
{
Timer checkTimer;
string curPass = "";
string pass = "mypass"; // take it from your config file
public LockScreen()
{
    InitializeComponent();
    this.KeyPreview = true;

    this.WindowState = FormWindowState.Normal;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Bounds = Screen.PrimaryScreen.Bounds;
    this.ShowInTaskbar = false;
    double OpacityValue = 4.0 / 100;
    this.Opacity = OpacityValue;

    // Add a keypress event
    this.KeyPress += LockScreen_KeyPress;

    checkTimer = new Timer();
    checkTimer.Interval = 1000;
    checkTimer.Tick += checkTimer_Tick;
}

void LockScreen_KeyPress(object sender, KeyPressEventArgs e)
{
    checkTimer.Stop();
    curPass += e.KeyChar.ToString();

    if (curPass == pass)      
        this.Close();
    else
        checkTimer.Start();
}

void checkTimer_Tick(object sender, EventArgs e)
{
    curPass = "";  //resets every second
}
}

答案 3 :(得分:0)

private const string Password = "Foobar";
private readonly StringBuilder _builder = new StringBuilder();

private void LockForm_KeyPress(object sender, KeyPressEventArgs e)
{
    //Check if the key pressed is a control character (e.g. alt/enter et cetera)
    //and return if that is the case
    //Also make sure to reset the stringbuilder's buffer now and then,
    //if too much input is generated
    _builder.Append(e.KeyChar);

    if (_builder.ToString().EndsWith(Password))
    {
        //Correct password has been entered
    }
}