Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track whether a user typed an specific "word" on an WinForm

Tags:

c#

winforms

I've a ScreenLocker winform application. It's full-screen and transparent. It unlocks the screen when user presses Ctrl+Alt+Shift+P.

But I want it more dynamic. I would like to let a user set his own password in a config file.

For example, he set his password mypass. My problem is- how can I track whether he typed 'mypass' on that form?

I don't want to have any textbox or button on the form. Help please.

Here is my current code-

    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();
        }
    }
like image 291
s.k.paul Avatar asked Sep 28 '22 06:09

s.k.paul


2 Answers

You can store the typed letters in a variable, then check it on enter keydown event. Here is the working sample-

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.
        }
    }
}
like image 68
Code It Avatar answered Oct 05 '22 06:10

Code It


I think the most elegant and robust solution is to use the reactive extension framework.

PM> Install-Package Rx-Main 

Key pressed are stored in a buffered observable sequence. When the buffer matches the password an action is taken (in the example it's a message box)

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())));
    }
like image 36
Paolo Costa Avatar answered Oct 05 '22 07:10

Paolo Costa