none
Detecting a key press? RRS feed

  • Question

  • Hello, I am trying to detect when a specific key is pressed. Here is my current code:

        Private Sub Form1_Keydown(sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
            If e.KeyCode = System.Windows.Forms.Keys.Alt Then
                MessageBox.Show("alt key pressed")
            End If
        End Sub
    Whenever I try to use e.KeyCode = Keys.Enter, I receive an error that states 'Enter is not a property of Keys'. When I use the code above there is no response at all. I have many questions as how it should fucntion. Will it be whenever the alt key is pressed? Do I have to call the function? I am trying to allow the user to save changes to an access database file whenever they press the alt key. Thanks. 
    Friday, January 24, 2020 12:16 PM

Answers

  • Hey Karen, I tried the code. However, on the line Keys.Enter or Keys.Menu I get the error:

    'Menu/Enter' is not a member of 'Keys'.  I have double and triple checked that I have the current parameters on the declaration of the sub. How come it doesn't recognize 'Enter' as a key? The autofill only allows the options .Equals or .ReferenceEquals

    Am I missing some sort of import?

    Thanks.

    For the record the code I provided was in a brand new VB.NET project and tested it. Then just now I placed the code into an older project and as I knew before testing it the code worked perfectly.

    So if it's not working in your Windows form project you must be doing something to interfere.  To test this create a new project and in the default form add my code for both KeyPreview and the event. Compile and run.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Lucky Hyena Friday, January 24, 2020 9:44 PM
    Friday, January 24, 2020 6:44 PM
    Moderator

All replies

  • This will work

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Menu Then
            'YourCode
            e.Handled = True
        ElseIf e.KeyCode = keys.Enter Then
            'YourCode
            e.Handled = True
        End If
    
    End Sub

    You may also need to have this line in say form load

    KeyPreview = True


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Friday, January 24, 2020 12:41 PM
    Moderator
  • Hey Karen, I tried the code. However, on the line Keys.Enter or Keys.Menu I get the error:

    'Menu/Enter' is not a member of 'Keys'.  I have double and triple checked that I have the current parameters on the declaration of the sub. How come it doesn't recognize 'Enter' as a key? The autofill only allows the options .Equals or .ReferenceEquals

    Am I missing some sort of import?

    Thanks.

    Friday, January 24, 2020 6:38 PM
  • Hey Karen, I tried the code. However, on the line Keys.Enter or Keys.Menu I get the error:

    'Menu/Enter' is not a member of 'Keys'.  I have double and triple checked that I have the current parameters on the declaration of the sub. How come it doesn't recognize 'Enter' as a key? The autofill only allows the options .Equals or .ReferenceEquals

    Am I missing some sort of import?

    Thanks.

    For the record the code I provided was in a brand new VB.NET project and tested it. Then just now I placed the code into an older project and as I knew before testing it the code worked perfectly.

    So if it's not working in your Windows form project you must be doing something to interfere.  To test this create a new project and in the default form add my code for both KeyPreview and the event. Compile and run.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Lucky Hyena Friday, January 24, 2020 9:44 PM
    Friday, January 24, 2020 6:44 PM
    Moderator
  • It seems that my code is somehow interfering, it worked on a brand new project. Thanks, I'll see if I can somehow isolate the source of the problem.
    Friday, January 24, 2020 9:44 PM
  • It seems that my code is somehow interfering, it worked on a brand new project. Thanks, I'll see if I can somehow isolate the source of the problem.

    I managed to find what was causing this. Whenever I used Keys.Enter it was referencing the Keys item in the ADOX library. This was due to me previously using the ADOX library to establish primary and foreign keys for some tables I created in a database file. The fix for this was to specifically use:

            If e.KeyCode = System.Windows.Forms.Keys.F1 Then
                MsgBox("Saved")
                e.Handled = True
            End If

    Instead of:

            If e.KeyCode = Keys.F1 Then
                MsgBox("Saved")
                e.Handled = True
            End If
    Also, if anyone believes this sub isn't working i.e. the action is not being followed through when the specified key is pressed, be sure to use KeyPreview = True in the Sub that you wish to detect the key press at.
    Friday, January 24, 2020 11:17 PM