Solved

SendKeys issue

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's 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 (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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