Results 1 to 5 of 5

Thread: Check if a key is currently pressed?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Check if a key is currently pressed?

    How would I check if a key is currently being pressed?

    Also how would I detect if a key is pressed without using the KeyDown event. The reason I am not using the keydown event is when the user holds down a key it calls the event, waits a second than keeps calling it. I do not want the wait period if a user is holding down a key.

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

    Re: Check if a key is currently pressed?

    The KeyDown event is what detects when a key is depressed so that's what you use. If you don't want to detect multiple events then you simply add a bit of logic to ignore all but the first one:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private ignoreKeyDown As Boolean = False
    4.  
    5.     Private Sub Form1_KeyDown(ByVal sender As Object, _
    6.                               ByVal e As KeyEventArgs) Handles Me.KeyDown
    7.         If Not Me.ignoreKeyDown Then
    8.             Me.ignoreKeyDown = True
    9.  
    10.             'Process the key here.
    11.         End If
    12.     End Sub
    13.  
    14.     Private Sub Form1_KeyUp(ByVal sender As Object, _
    15.                             ByVal e As KeyEventArgs) Handles Me.KeyUp
    16.         Me.ignoreKeyDown = False
    17.     End Sub
    18.  
    19. End Class
    As for the first question, I'm not aware of a way to do that with managed code. I think you'd have to use the GetAsyncKeyState function from the Windows API. If you search the forum you will undoubtedly find examples.
    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
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: Check if a key is currently pressed?

    Sorry what I meant was I do want it to detect multiple events. I just do not want that wait period in between the detection. Like how you hold down a key in a chat box it prints the key, than waits a second and continues printing the key if it is still pressed. I just don't want that one second wait.

  4. #4
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Check if a key is currently pressed?

    Use this declaration at the top of your form:

    Code:
        Private Declare Function GetKeyState Lib "user32" (ByVal keyCode As Integer) As Integer
    Then create a Timer with an interval of 1 and use this in the Tick event.

    Code:
            If GetKeyState(Keys.Left) < 0 Then 'Do Stuff
    Keys.Left can be any key you want and whatever action is in 'Do Stuff will happen the instant the key is pressed and will not pause to repeat.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

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

    Re: Check if a key is currently pressed?

    Sorry what I meant was I do want it to detect multiple events. I just do not want that wait period in between the detection. Like how you hold down a key in a chat box it prints the key, than waits a second and continues printing the key if it is still pressed. I just don't want that one second wait.
    In that case you'd have to ignore system settings, because that delay is part of Windows. It helps prevent people accidentally registering multiple key presses if they're a bit slow to release the key.

    Start a Timer on the KeyDown event and then Stop it on the KeyUp event, then handle the Tick event of your Timer.
    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

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