none
Keyboard Combination not working RRS feed

  • Question

  • Hello,

    I want to do a Keyboard combination to activate a Button on the Main Form. I have googled the question "How to make Keyboard combinations" and this example keeps popping up.

    Private Sub FrmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.Alt And e.Control = True Then
                'frmSearch.Show()
                MsgBox(vbInformation & "Ctrl + Alt combination")
            End If
    End Sub

    If I understand this right if I press the Control and Alt button together the message shows. However, this isn't the case.

    How come this isn't working?


    MRM256



    • Edited by MRM256 Wednesday, February 12, 2020 9:54 PM
    Wednesday, February 12, 2020 9:52 PM

Answers

  • Hi Karen,

    This seems overly complicated. Suppose you want a Key combination:

     Private Sub FrmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = e.Alt And e.Control And Keys.F12 = True Then
                FrmLookUpDB.Show()
                MsgBox(vbInformation & "Ctrl + Alt + F12 combination")
            End If
     End Sub

    In otherwords it should easily coded.

    Thanks


    MRM256

    Actually the thing is trying to only capture the two keys is problematic while using ALT+CTRL+F12 is easier.

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        If keyData = (Keys.Control Or Keys.Alt Or Keys.F12) Then
            MessageBox.Show("Do something")
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function


    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 MRM256 Thursday, February 13, 2020 4:31 PM
    Thursday, February 13, 2020 10:39 AM
    Moderator

All replies

  • You must test e.Alt (or Keys.Menu)

    and add

    Me.KeyPreview = True
    (if child controls)


    • Edited by Castorix31 Wednesday, February 12, 2020 10:24 PM
    • Proposed as answer by simonb549 Thursday, February 13, 2020 8:37 AM
    Wednesday, February 12, 2020 10:14 PM
  • Were would the Me.KeyPreview = True go?

    I haven't seen this before.

    Thanks,


    MRM256

    Wednesday, February 12, 2020 11:33 PM
  • This will work when pressing CTRL+ALT, no need for KeyPreview.

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Dim keyComboArray() As Integer = {393233, 393234}
    
        If keyComboArray.Contains(keyData) Then
            MessageBox.Show("CTRL+ALT pressed")
            Return True
        End If
    
        Return MyBase.ProcessCmdKey(msg, keyData)
    
    End Function


    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

    Thursday, February 13, 2020 12:12 AM
    Moderator
  • Hi Karen,

    This seems overly complicated. Suppose you want a Key combination:

     Private Sub FrmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = e.Alt And e.Control And Keys.F12 = True Then
                FrmLookUpDB.Show()
                MsgBox(vbInformation & "Ctrl + Alt + F12 combination")
            End If
     End Sub

    In otherwords it should easily coded.

    Thanks


    MRM256

    Thursday, February 13, 2020 3:03 AM
  • This seems overly complicated. Suppose you want a Key combination:

     Private Sub FrmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = e.Alt And e.Control And Keys.F12 = True Then
                FrmLookUpDB.Show()
                MsgBox(vbInformation & "Ctrl + Alt + F12 combination")
            End If
     End Sub

    In otherwords it should easily coded.



    If you really want to detect it in KeyDown, and your form does not have other focusable controls, then check this too:

       If e.Control And e.Alt And e.KeyCode = Keys.F12 Then

          MsgBox("Ctrl + Alt + F12 combination", vbInformation)

       End If

     

    If your form has a menu, you can define the ShortcutKeys for each item in Form Designer. Then such code is not needed.



    • Edited by Viorel_MVP Thursday, February 13, 2020 7:31 AM
    Thursday, February 13, 2020 7:05 AM
  • Were would the Me.KeyPreview = True go?

    I haven't seen this before.

    Thanks,


    MRM256

    if you have children in the Form, it is needed

    see : https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.keypreview?view=netframework-4.8

    Thursday, February 13, 2020 8:39 AM
  • Hi Karen,

    This seems overly complicated. Suppose you want a Key combination:

     Private Sub FrmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = e.Alt And e.Control And Keys.F12 = True Then
                FrmLookUpDB.Show()
                MsgBox(vbInformation & "Ctrl + Alt + F12 combination")
            End If
     End Sub

    In otherwords it should easily coded.

    Thanks


    MRM256

    Actually the thing is trying to only capture the two keys is problematic while using ALT+CTRL+F12 is easier.

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        If keyData = (Keys.Control Or Keys.Alt Or Keys.F12) Then
            MessageBox.Show("Do something")
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function


    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 MRM256 Thursday, February 13, 2020 4:31 PM
    Thursday, February 13, 2020 10:39 AM
    Moderator
  • Karen,

    This worked :-).

    All I was trying to do was make a hidden (Easter Egg) function that would allow me to open up a form to check data inside the LocalDB.

    Thanks,


    MRM256

    Thursday, February 13, 2020 4:34 PM
  • Karen,

    This worked :-).

    All I was trying to do was make a hidden (Easter Egg) function that would allow me to open up a form to check data inside the LocalDB.

    Thanks,


    MRM256

    Cool in regards to an Easter Egg.


    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

    Thursday, February 13, 2020 5:22 PM
    Moderator
  • Karen,

    I hope you don't mind, but I modified your code, so other Easter Eggs could be added. 

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message,
                                                   ByVal keyData As Keys) _
                                                   As Boolean
            'Purpose:       'Easter Egg Section:
            '                   Ctrl + Alt + F12.
            '                       Shows FrmLookUpDB                    
            'Parameters:    ByRef msg As Message,
            '               ByVal keyData As Keys.
            'Returns:       True - If a defined Key combination is 
            '                       entered, False; otherwise.
    
            'A neater way to add additional Easter Eggs to FrmMain
            Select Case keyData
                Case Keys.Control Or Keys.Alt Or Keys.F12
                    'FrmMain must be active to open the
                    'FrmLookUpDB form.
                    Cursor = Cursors.WaitCursor
                    FrmLookUpDB.Show()
                    Cursor = Cursors.Default
                    Return True
                    'Another Easter Egg definition
                    'Case Keys.Control Or Keys.Alt Or Keys.F11
            End Select
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function

    Thanks,


    MRM256

    Thursday, February 13, 2020 9:50 PM