hey, folks, thanks for clicking :)

i want to assign a pressed key to a button, i clicked before, similar to what we know from computergames, when we change the key bindings.
For example: click button1 -> the application is waiting for any key to be pressed -> we push ctrl on the keyboard -> ctrl is assigned to button1-> button1.text changes to ctrl
same procedure with button 2 ....and so on

i googled for hours and cant find a solution :(

Recommended Answers

All 4 Replies

Here's one suggestion. This example uses a form with KeyPreview = True. You need this in order to process the keystrokes.

Public Class Form1

    Private CurrBtn As Button = Nothing

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        For Each btn As Button In Me.Controls.OfType(Of Button)()
            AddHandler btn.Click, AddressOf Button_Click
        Next

    End Sub

    Private Sub Form1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

        If IsNothing(CurrBtn) Then Exit Sub

        Me.Text = e.KeyCode

        Select Case e.KeyCode
            Case Keys.ControlKey
                CurrBtn.Text = "CTRL"
            Case Keys.A To Keys.Z
                CurrBtn.Text = Chr(e.KeyCode)
        End Select

        CurrBtn = Nothing

    End Sub

    Private Sub Button_Click(sender As System.Object, e As System.EventArgs)

        Dim btn As Button = sender

        If btn.Text = "" Then
            CurrBtn = btn
        End If

    End Sub

End Class

Precondition - all the buttons you want to dynamically assign must have the Text property set to the null string initially. The form load sub attaches the same handler to all blank buttons. CurrBtn allows you too select a button then activate it by pressing a key. You may also want to provide visual feedback by changing the button colour while it is active.

Suggestions for improvement - add the ability to re-assign a key that has already been assigned. You could do this by testing (in the common click handler) if the CTRL key was being pressed while the button was clicked. So click on a blank button means assign the next keystroke to the button. CTRL-click on a button, blank or otherwise means the same thing. You might also add the ability to unselect a button. If you followed my earlier suggestion of changing the BG colour you could determine if a currently selected button has been clicked again, in which case you could just unselect it and change thee BG colour back to default.

Also, you might want to ensure that you ignore assigning keys for values that have already been assigned to a button.

Thank you sooo much!
Now i need to understand whats happening there exactly:)
Thank you!

I hope i can give something back to the community soon :)

You have to be able to remember which button was selected to assign a keystroke. The easiest way is to use a class level variable (CurrBtn). Setting KeyPreview to True allows you to examine the key presses. Because the buttons will all have essentially the same function you can use the same handler. If you have any other questions about the code I posted feel free to ask them here.

You are going to want to use the buttons for something after you assign the keycodes to them so you will need some way to determine whether you are in Assign mode or not. Perhaps another Class variable with a button that will toggle the app between Assign and Use modes. You can test the mode in the button click handler and base your processing on that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.