Capturing a series of keystrokes at any time
-
My question is rather simple. We all have played computer games, or other console-based games, where at any given moment you can type in "thereisnotry", or something similar and it activates a new feature, be it a class method giving you maximum health, or whatever. How exactly can that be done in VB.NET? Line, at any time if I type in "magic string" I can catch that event (is that an event?) and handle it...
Jonathan Sampson www.SampsonResume.com
-
My question is rather simple. We all have played computer games, or other console-based games, where at any given moment you can type in "thereisnotry", or something similar and it activates a new feature, be it a class method giving you maximum health, or whatever. How exactly can that be done in VB.NET? Line, at any time if I type in "magic string" I can catch that event (is that an event?) and handle it...
Jonathan Sampson www.SampsonResume.com
What type of application is it? In a Windows Forms application you could look at using the KeyPress event. You will, though, need to handle the event for each control on the form
Private m\_strCapturedString As String = "" Private Sub TextBox1\_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If System.Char.IsControl(e.KeyChar) Then Exit Sub End If m\_strCapturedString += e.KeyChar If m\_strCapturedString.Length = 10 Then MessageBox.Show(m\_strCapturedString) End If End Sub Private Sub Button1\_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress If System.Char.IsControl(e.KeyChar) Then Exit Sub End If m\_strCapturedString += e.KeyChar If m\_strCapturedString.Length = 10 Then MessageBox.Show(m\_strCapturedString) End If End Sub
-
My question is rather simple. We all have played computer games, or other console-based games, where at any given moment you can type in "thereisnotry", or something similar and it activates a new feature, be it a class method giving you maximum health, or whatever. How exactly can that be done in VB.NET? Line, at any time if I type in "magic string" I can catch that event (is that an event?) and handle it...
Jonathan Sampson www.SampsonResume.com
You don't have to handle the keydown event of every control. You just have to do it in the Form's KeyDown event. But, in order for it to work, you have to turn on (True) the Form's KeyPreview property. There is no event for the typing of a string of characters, only one keystroke. Your Form's KeyPress event handler code then has to keep track of the last "x" number of keys that were hit and compare them to the string you want as your "cheat" string.
Dave Kreskowiak Microsoft MVP - Visual Basic