Button code change when SHIFT is been pressed

Posted on 2007-07-30
Last Modified: 2010-04-23
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

Question by:Marcusw
    LVL 27

    Expert Comment

    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
    LVL 96

    Expert Comment

    by:Bob Learned

    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

    LVL 34

    Accepted Solution

    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

    LVL 96

    Expert Comment

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now