[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • 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

thanks
0
Marcusw
Asked:
Marcusw
  • 2
1 Solution
 
planoczCommented:
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
0
 
Bob LearnedCommented:
Alternative:

Public Class Keyboard

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

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

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

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

End Class

Example:
If Keyboard.ShiftKey Then
End If

Bob
0
 
SanclerCommented:
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
        Debug.WriteLine(Button1.Tag.ToString)
    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

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

Bob
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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