Track whether a user typed a specific word on WinForm

I have a winform ScreenLocker application. It is fullscreen and transparent. It unlocks the screen when the user press Ctrl+Alt+Shift+P

But I want it to be more dynamic. I want the user to set their own password in the configuration file.

For example, he set his password mypass . My problem: how can I track if he typed "mypass" in this form?

I do not want to have a text box or button in the form. Help me 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(); } } 
+5
source share
4 answers

You can save the entered letters in a variable, and then check it when you enter the keydown event. Here is a 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. } } } 
+2
source

I think the most elegant and reliable solution is to use a reactive expansion platform.

 PM> Install-Package Rx-Main 

Keystrokes are stored in a buffered observable sequence. When the buffer matches the password, an action is performed (in the example, this is 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()))); } 
+2
source

It works

 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 } } 
0
source
 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 (eg alt/enter et cetera) //and return if that is the case //Also make sure to reset the stringbuilder buffer now and then, //if too much input is generated _builder.Append(e.KeyChar); if (_builder.ToString().EndsWith(Password)) { //Correct password has been entered } } 
0
source

All Articles