• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Button code change when SHIFT is been pressed

is it possible to make a button on  a form run a different piece of code if SHIFT is held down when it was pressed

  • 2
1 Solution
Hi Marcusw,
Use this to catch any key pressed and tag it to your Button.

  Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Const WM_KEYDOWN As Integer = &H100
        Const WM_SYSKEYDOWN As Integer = &H104

        If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
            Select Case (keyData)
                Case Keys.A To Keys.Z                                '<--- Catch the Keys you want
                    MessageBox.Show("You pressed " & ChrW(keyData))  '<--- Then do something
                Case Keys.Right
                    Me.Text = "Right Arrow Captured"
                Case Keys.Left
                    Me.Text = "Left Arrow Captured"
                Case Keys.Down
                    Me.Text = "Down Arrow Captured"
                Case Keys.Up
                    Me.Text = "Up Arrow Captured"
                Case Keys.Tab
                    Me.Text = "Tab Key Captured"
                Case (Keys.Control Or Keys.N)
                    Me.Text = "<CTRL> + N Captured"
                Case (Keys.Alt Or Keys.Z)
                    Me.Text = "<ALT> + Z Captured"
                Case Keys.NumPad0
                    Me.Text = "Num Pad '0' Captured"
                Case Keys.Delete
                    MessageBox.Show("Delete Captured")
                Case Keys.NumLock
            End Select
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
Bob LearnedCommented:

Public Class Keyboard

    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer

    Public Shared ReadOnly Property ControlKey() As Boolean
            Return GetAsyncKeyState(Keys.ControlKey) And &H8000
        End Get
    End Property

    Public Shared ReadOnly Property AltKey() As Boolean
            Return GetAsyncKeyState(Keys.Menu) And &H8000
        End Get
    End Property

    Public Shared ReadOnly Property ShiftKey() As Boolean
            Return GetAsyncKeyState(Keys.ShiftKey) And &H8000
        End Get
    End Property

End Class

If Keyboard.ShiftKey Then
End If

Or use Control.ModifierKeys in the Button's mousedown and mouseup events

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub

    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        If Control.ModifierKeys = Keys.Shift Then
            Button1.Tag = "Shift"
        End If
    End Sub

    Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
        Button1.Tag = "None"
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Button1.Tag = "None"
    End Sub

Bob LearnedCommented:
Thanks, Roger, that was what I was looking for (too sleepy, I guess).

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now