Thread: combined keyPresses

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    combined keyPresses

    Can you detect a combination of keys pressed with a KeyPressed event handler on a Windows.Form?
    I want to detect the UP and RIGHT arrow for example being pressed together (to move something diagonally)

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    hi - i had a similar problem and posed the same question elsewhere.

    my reply was to use

    Code:
    public static bool IsKeyDown(Keys key)
            {
                return GetAsyncKeyState((int)key) < 0;
            }
    
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern short GetAsyncKeyState(int vkey);
    then use something like

    Code:
    bool temp;
    
                if ((temp = IsKeyDown(Keys.Right)) && (temp = IsKeyDown(Keys.Left)))
    
                //do something here depending on keys ( right & left at same time in this case)
    this will return the state of every key on the keyboard allowing you to check for simultaneous key presses.

    hope this helps

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yup. That works fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 10:07 PM
  2. vVv - Keypresses (again)
    By Skarr in forum Linux Programming
    Replies: 6
    Last Post: 08-06-2002, 06:01 AM
  3. How to scan for keypresses?
    By Unregistered in forum Linux Programming
    Replies: 4
    Last Post: 07-09-2002, 01:18 PM
  4. Keyboard INT 9 handler (Handles multiple keypresses!) Problem
    By zhopon in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-16-2001, 10:39 AM
  5. Replies: 0
    Last Post: 12-16-2001, 10:36 AM