Recognising key sequence

jwh

Well-known member
Joined
Aug 18, 2006
Messages
155
Programming Experience
3-5
Good evening.

I need my MDI based application to respond to a certain sequance of keystrokes, no matter which child form/control has the focus.
The sequence of keys I need to recongnise is "%CLK"

Does anybody have any suggestions where I could start on this one as I have no idea!
 
hm, maybe you could add an event/event handler to the parent form, so it will catch it no matter what. alternatively what about creating a module/class (what is the EXACT difference between the two, and what are disadvantages/advantages of each) that has the event handler in it, so it will always be listening for it.

the only other option i can think of would be to create the code to handle the combination in each and every form in your project.

hope that helps

regards

adam
 
Set the KeyPreview property of your parent form to True and add code like this:
VB.NET:
Private keySequence As String

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    If e.KeyChar = "%"c Then
        'Record that the first key was detected.
        Me.keySequence = "%"
    ElseIf Char.ToLower(e.KeyChar) = "c"c AndAlso Me.keySequence = "%" Then
        'Record that the second key was detected.
        Me.keySequence = "%c"
    ElseIf Char.ToLower(e.KeyChar) = "l"c AndAlso Me.keySequence = "%c" Then
        'Record that the third key was detected.
        Me.keySequence = "%cl"
    ElseIf Char.ToLower(e.KeyChar) = "k"c AndAlso Me.keySequence = "%cl" Then
        'Reset the key sequence.
        Me.keySequence = String.Empty

        'Announce that the entire sequence was detected.
        MessageBox.Show("Key sequence %CLK detected.")
    Else
        'The key is not part of the sequence so reset.
        Me.keySequence = String.Empty
    End If
End Sub
 
That sorted the problem nicely - thanks!
 
Back
Top