Solved

SendKeys issue

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

16 Experts available now in Live!

Get 1:1 Help Now