Results 1 to 5 of 5

Thread: [RESOLVED] Capture key press for hotkey

Threaded View

  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.

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