Results 1 to 3 of 3

Thread: [RESOLVED] Capture Multiple Key Presses

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Resolved [RESOLVED] Capture Multiple Key Presses

    Hey guys!

    I'm looking to trigger an event in my form that requires users to press a certain sequence of keys on my form, say red or blue. I'm unsure how to do this.

    I know how to check for since key instances but not multiples so any help would be greatly appreciated as always.

    Cheers.
    My Blog.

    Ryan Jones.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Capture Multiple Key Presses

    You simply need to remember what's gone before, e.g.
    csharp Code:
    1. private int keyPresses = 0;
    2.  
    3. private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    4. {
    5.     switch (e.KeyChar)
    6.     {
    7.         case 'a':
    8.             // 'a' is always the first key in the sequence.
    9.             this.keyPresses = 1;
    10.             break;
    11.         case 'b':
    12.             // if 'b' follows 'a' then progress, otherwise start over.
    13.             this.keyPresses = (this.keyPresses == 1 ? 2 : 0);
    14.             break;
    15.         case 'c':
    16.             // if 'c' follows 'a' and 'b' then progress, otherwise start over.
    17.             this.keyPresses = (this.keyPresses == 2 ? 3 : 0);
    18.             break;
    19.         case 'd':
    20.             // if 'd' follows 'a', 'b' and 'c' then complete the sequence.
    21.             if (this.keyPresses == 3)
    22.             {
    23.                 MessageBox.Show("You pressed abcd!");
    24.             }
    25.  
    26.             // Start over.
    27.             this.keyPresses = 0;
    28.             break;
    29.         default:
    30.             // Start over on any other key.
    31.             this.keyPresses = 0;
    32.             break;
    33.     }
    34. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Capture Multiple Key Presses

    That worked perfectly. I figured it would be something along those lines but I couldn't get it right. Looks like my more complex solution was at fault. Oh well.

    Thanks!

    Edit: If anyone is interested here is the code I used. it's a slightly modified version of the one above and should give more customizability.

    csharp Code:
    1. private Dictionary<int, char> secret_code = new Dictionary<int, char>()
    2. {
    3.   {0, 't'},
    4.   {1, 'e'},
    5.   {2, 's'},
    6.   {3, 't'}
    7. };
    8.  
    9. private void frmMain_KeyPress(object sender, KeyPressEventArgs e)
    10. {
    11.   if (e.KeyChar == secret_code[key_presses])
    12.   {
    13.     if (key_presses == (secret_code.Count() - 1))
    14.     {
    15.       this.key_presses = 0;
    16.       MessageBox.Show("You sound the secret!", "Congratulations!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    17.     }
    18.     else
    19.     {
    20.       ++this.key_presses;
    21.     }
    22.   }
    23.   else
    24.   {
    25.     this.key_presses = 0;
    26.   }
    27. }
    Last edited by sciguyryan; Jan 12th, 2010 at 02:57 PM. Reason: Added code.
    My Blog.

    Ryan Jones.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width