Solved

SendKeys issue

Posted on 2007-03-21
3
261 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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