none
How do i assign a hotkey to a subroutine in VB?

    Question

  • I have a table and when the cursor is in a given cell I want to be able to press a hotkey (in this case Ctrl-D) to run a subroutine.

    Friday, July 08, 2016 8:04 AM

Answers

  • That's got me on the right track.

    I'm using this code:

        Private Sub MyTable_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyTable.KeyPress
            MsgBox("Table - KeyPress")
        End Sub

        Private Sub MyTable_KeyDown(sender As Object, e As KeyEventArgs) Handles MyTable.KeyDown
            MsgBox("Table - KeyDown")
        End Sub

        Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            'MsgBox("Form - KeyDown")
        End Sub

        Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp

            Select Case e.KeyData
                Case (Keys.Control + Keys.D)
                    MsgBox("Yippee!")
                    'filter other keys here
                Case Else
                    MsgBox("Form - KeyUp" + Str(e.KeyValue))
            End Select
        End Sub

    The "MyTable" subs sometimes work, but the "Form1" ones work all the time, albeit anywhere on the form, including the table, as expected.

    I'm thinking maybe I don't always have the table in focus, even though I can type into its cells.

    Thanks again,

    Roger.

    • Marked as answer by Roger T B Sunday, July 10, 2016 11:40 AM
    Friday, July 08, 2016 3:13 PM

All replies

  • Hi,

    can you explain in detail what you are trying to do?

    You created an application (What kind of application? Windows Forms? WPF? UWP?) which shows a table and now oyu want to create a hotkey for the user?

    Or do you work inside Visual Studio and you have some kind of table (e.g. you did a select query on a database) and now you want to have this? That is a uncommon request - Visual Studio is just a integrated development environment. Its purpose is not to have small subroutines to run on a data table row or so.)

    Or do you use some other software e.g. excel in which you have a table with data and now you want to execute some functions with a hotkey (So it is Visual Basic for Applications - VBA). In this case I would recommend to ask inside an Office / Excel forum instead.

    With kind regards,

    Konrad

    Friday, July 08, 2016 8:14 AM
    Moderator
  • Hi Konrad, 

    Thanks for your quick response.  Basically I have written an application in VBA in Excel and I'm now translating it to work as a stand-alone using Studio 2015.

    So, I'm writing a Form-based application, I've added a table to the form to emulate the Excel form, I've managed to load a CSV file, parse various fields and now I want to add hotkeys to enable the user to perform operations on the data in the table. For example Ctrl-D will highlight the row where the cursor is and set a flag marking that row for deletion.

    Hope this makes sense!

    Cheers,

    Roger

    Friday, July 08, 2016 9:18 AM
  • Hi,

    the forms have a Property called KeyPreview - that should be set to true. (You can do this in the properties of the form. So inside the designer you can select the form and then go through the properties.

    Then you could use the KeyDown event. That gives you a KeyEventArgs Argument so you can check that the Control Property of this argument is true and that the KeyCode Property is Keys.D.

    I don't have a WindowsForms application ready to check the exact steps and provide some example code (Which is even harder for me, because I am mainly a C# developer). But the KeyDown event could be used on the table or on the form itself. If you do it on the table, then it only works if the focus is on the table. On the form it will work as long as the form has the focus.  

    I hope that this helped a little bit even that I was unable to give exact code.

    With kind regards,

    Konrad

    Friday, July 08, 2016 9:50 AM
    Moderator
  • That's got me on the right track.

    I'm using this code:

        Private Sub MyTable_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyTable.KeyPress
            MsgBox("Table - KeyPress")
        End Sub

        Private Sub MyTable_KeyDown(sender As Object, e As KeyEventArgs) Handles MyTable.KeyDown
            MsgBox("Table - KeyDown")
        End Sub

        Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
            'MsgBox("Form - KeyDown")
        End Sub

        Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp

            Select Case e.KeyData
                Case (Keys.Control + Keys.D)
                    MsgBox("Yippee!")
                    'filter other keys here
                Case Else
                    MsgBox("Form - KeyUp" + Str(e.KeyValue))
            End Select
        End Sub

    The "MyTable" subs sometimes work, but the "Form1" ones work all the time, albeit anywhere on the form, including the table, as expected.

    I'm thinking maybe I don't always have the table in focus, even though I can type into its cells.

    Thanks again,

    Roger.

    • Marked as answer by Roger T B Sunday, July 10, 2016 11:40 AM
    Friday, July 08, 2016 3:13 PM