Results 1 to 5 of 5

Thread: [RESOLVED] Capture key press for hotkey

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Capture key press for hotkey

    Hello, I'm making an application that will execute some functions on user defined key presses.

    The user needs to set the keys they want to fire the function in a textbox. I've set the textbox to Read-Only and am trying to make the code that will specify the key combinations used that are displayed in the textbox. So for example; if a user presses Ctrl + Alt + Shift + E, the textbox will show this.

    I'm using the KeyDown and KeyUp events that come with a textbox. The textbox will loose focus once a control key (Ctrl or Alt) AND an alphanumeric key (such as E) is pressed. Shift is optional.

    I need to store this data, so that it can later be fed into the GetAsyncKeyState function that will listen in a timer.

    I'm having some troubles getting the KeyDown and KeyUp detect whether is was the left or right Ctrl, Alt and Shift keys pressed as well.

    Here's my code currently:
    Code:
        Private ctrlKeyDw As Boolean = False
        Private altKeyDw As Boolean = False
        Private shiftKeyDw As Boolean = False
    
        Private Sub txtCaptureKey_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCaptureKey.KeyDown
    
            If e.KeyCode = Keys.ControlKey Then
                ctrlKeyDw = True
            End If
    
            If e.KeyCode = Keys.Alt Then
                altKeyDw = True
            End If
    
            If e.KeyCode = Keys.Shift Then
                shiftKeyDw = True
            End If
    
            updateText()
    
        End Sub
    
        Private Sub updateText()
    
            txtCaptureKey.Text = ""
    
            If ctrlKeyDw Then
                txtCaptureKey.Text = "Ctrl + " & txtCaptureKey.Text
            End If
    
            If altKeyDw Then
                txtCaptureKey.Text = "Alt + " & txtCaptureKey.Text
            End If
    
            If shiftKeyDw Then
                txtCaptureKey.Text = "Shift + " & txtCaptureKey.Text
            End If
    
            If txtCaptureKey.Text = "" Then
                txtCaptureKey.Text = "None"
            End If
    
        End Sub
    
        Private Sub txtCaptureKey_KeyUp(sender As Object, e As KeyEventArgs) Handles txtCaptureKey.KeyUp
    
            If e.KeyCode = Keys.ControlKey Then
                ctrlKeyDw = False
            End If
    
            If e.KeyCode = Keys.Alt Then
                altKeyDw = False
            End If
    
            If e.KeyCode = Keys.Shift Then
                shiftKeyDw = False
            End If
    
            updateText()
    
        End Sub
    At the moment it's only detecting when Ctrl is pressed. I can't figure out why Shift and Alt are not being detected.
    Last edited by Slyke; Dec 30th, 2013 at 08:00 PM.

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

    Re: Capture key press for hotkey

    Don't the KeyCode property to detect modifier keys in combination with other keys.
    vb.net Code:
    1. Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    2.     Dim depressedKeys As New List(Of String)
    3.  
    4.     If e.Control Then
    5.         depressedKeys.Add("Ctrl")
    6.     End If
    7.  
    8.     If e.Shift Then
    9.         depressedKeys.Add("Shift")
    10.     End If
    11.  
    12.     If e.Alt Then
    13.         depressedKeys.Add("Alt")
    14.     End If
    15.  
    16.     If e.KeyCode >= Keys.A AndAlso e.KeyCode <= Keys.Z Then
    17.         depressedKeys.Add(e.KeyCode.ToString())
    18.     End If
    19.  
    20.     Me.TextBox1.Text = String.Join("+", depressedKeys)
    21. End Sub
    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
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Capture key press for hotkey

    Thanks heaps for that jmcilhinney. Is there any reason that Lcontrol and RControl doesn't work when using KeyCode? Otherwise your code is working perfectly.

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

    Re: Capture key press for hotkey

    Quote Originally Posted by Slyke View Post
    Thanks heaps for that jmcilhinney. Is there any reason that Lcontrol and RControl doesn't work when using KeyCode? Otherwise your code is working perfectly.
    As far as I can tell from research on the web and my own testing, you can't detect the difference between the left and right Ctrl keys via Windows Forms keyboard events. I haven't checked but I'm guessing that the same is true of Shift and Alt keys. You would have to first detect a Ctrl key in the conventional way and then use the GetAsyncKeyState API to determine the state of the left and right keys.
    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Capture key press for hotkey

    That's a great idea. Will do just that.

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