Solved

SendKeys issue

Posted on 2007-03-21
3
257 Views
Last Modified: 2012-08-14
I'm trying to send keystrokes to activate context-menu events from one control to another.

I have a textbox, txtSearch which has the following procedure:

    Private Sub txtSearch_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyUp

        If e.Control = True Then

            trvInventory.InventoryTree_KeyUp(sender, e)

        End If ' e.Control = True

    End Sub

And trvInventory has the following procedure:

    Public Sub InventoryTree_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

        If Not (sender Is Me) Then

            Dim strKeys As String = ""

            If e.Control = True Then
                strKeys = "^"
            End If ' e.Control = True

            strKeys &= Chr(e.KeyCode)

            Me.Focus()

            SendKeys.SendWait(strKeys)

            CType(sender, Control).Focus()

        End If ' Not (sender Is Me)

    End Sub ' InventoryTree_KeyUp

However, the code does not activate the context-menu shortcut.  What am I doing wrong?
0
Comment
Question by:jake072
3 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18767339
Try sendkeys +{F10}
0
 
LVL 27

Accepted Solution

by:
planocz earned 500 total points
ID: 18768937
Add this code to your form and test it
With this function you can change it to find the key that you want.

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

Author Comment

by:jake072
ID: 18771160
planocz,

By using your code, and changing the sub's to friend in my control, it works perfectly...  Should've thought of that :)

My code:

    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.Control Or Keys.I)
                    trvInventory.ctxCreateItem_Click(Nothing, Nothing)

                Case (Keys.Control Or Keys.M)
                    trvInventory.trvInventory_DoubleClick(Nothing, Nothing)

                Case (Keys.Control Or Keys.D)
                    trvInventory.ctxDelete_Click(Nothing, Nothing)

                Case (Keys.Control Or Keys.Q)
                    trvInventory.ctxResetQty_Click(Nothing, Nothing)

                Case (Keys.Control Or Keys.C)
                    trvInventory.ctxCreateGroup_Click(Nothing, Nothing)

                Case (Keys.Control Or Keys.O)
                    trvInventory.ctxModifyGroup_Click(Nothing, Nothing)

                Case (Keys.Control Or Keys.G)
                    trvInventory.ctxDeleteGroup_Click(Nothing, Nothing)

                Case (Keys.Control Or Keys.N)
                    trvInventory.ctxManageDelete_Click(Nothing, Nothing)

                Case (Keys.Control Or Keys.R)
                    trvInventory.ctxResetPricing_Click(Nothing, Nothing)

            End Select ' (keyData)

        End If ' ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN))

        Return MyBase.ProcessCmdKey(msg, keyData)

    End Function ' ProcessCmdKey

Thanks!

Jake
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

22 Experts available now in Live!

Get 1:1 Help Now