C# Key Checking

Discussion in 'C#' started by TinyClone, Apr 12, 2006.

  1. TinyClone

    TinyClone New Member

    Joined:
    Apr 6, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Because there no forum for C# i'll post i here:

    I want to check whether a specific key is press a the moment.
    However, I can only find functions to check this on events.
    I want to check this when there's no event called.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    There is a forum for C# and I have moved the post to the correct section.
     
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    37
    Trophy Points:
    28
    When you are not in a key press event that does mean key is not pressed and when ever its pressed the event will be fired.
     
  4. TinyClone

    TinyClone New Member

    Joined:
    Apr 6, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    no its not actually. It fires only the first time.

    EDIT: I solved it now for 4 keys with an array which remembers the states.
     
  5. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    37
    Trophy Points:
    28
    Can you explain a bit more on what you mean fires for the first time. Do you mean it fires once for multiple key fire events.
     
  6. TinyClone

    TinyClone New Member

    Joined:
    Apr 6, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    it only fires when you press it down, not when it is pressed:
    When I keep the down key pressed it fires only once; I want to check if its down every 1/40 second.
    I did it though on the way described one post earlier.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    What event you are catching. KeyPress or KeyDown.
     
  8. TinyClone

    TinyClone New Member

    Joined:
    Apr 6, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    doesnt matter; i want to let it fire like 40 times a sec, and KeyPress only fires on press after its released; KeyDown only fires on pressing the key down (1 time).
     
  9. SCPP

    SCPP New Member

    Joined:
    May 26, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Well you could use the event approach and create a variable to store the state of your key. Detect when the keydown event indicates it has been pressed.

    E.g.

    Code:
    private bool ControlKeyDown = false;
    
    private void txtSendData_KeyDown(object sender, KeyEventArgs e)
    {
        if (KeyHandled = e.KeyCode == Keys.ControlKey)
        {     
             ControlKeyDown = true;
        }
    }
    
    private void txtSendBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.ControlKey)
        {
            ControlKeyDown = false;
        }
    }
    
    Then all you'd have to do is check your ControlKeyDown variable. It will get updated in realtime by the event handlers.
     
  10. night.rider

    night.rider New Member

    Joined:
    Jul 13, 2009
    Messages:
    14
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    create and implement Logic..
    Location:
    somewhere
    you cannot check the key char until tthe key is pressed and when any key is pressed a keyevent is fired..
    and this depends on you that which event u want to fire either event on pressing or after pressing or after key is up and so on..

    so u only need is in event is the args as (object sender, KeyEventArgs e)
    now u have different properties with 'e' and using intellisence check whether there is keychar prop so u write as follows..
    if(e.keychar<64 && e.keychar>91) to check if Capitol alphabet is pressed(i.e caps +alphabet) and so on..
    e.keychar returns ascii values of key pressed

    Check and do tel me how it runs.. m waiting
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Try GetAsyncKeyState().
     
  12. inno

    inno New Member

    Joined:
    Oct 21, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Try

    Code:
    [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Form[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ModifierKeys == [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Keys[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Control[/SIZE]
    to check if CTRL is pressed, for example.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice