Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27382

VS 2010 Trouble Sending KeyStrokes to User Started Application

$
0
0
Hi

It is preferrable to get a solution using VB.NET but C# is acceptable as I can always convert it ti VB.NET.

I am creating a "custom toolbar"with pre-programmed keyboard key combinations for a disabled person. The user first starts the "toolbar" and then starts any application with the "toolbar" displayed on the screen. I do not know the process name or PID of the user application. The user clicks a toolbar button and the appropriate key combination is sent to the user application. I have tried to use SendKeys and keybd_event and nothing works.

I have read that I must have a handle to the active window. All the examples I have read seem to require I know the proceess name to get the window handle. How do I do this if I do not know what process was started?

I have tried:

Code:

    Public Shared Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                                          Handles btnSend.Click
        Dim TopMosthWnd As IntPtr
        Dim OldActivehWnd As IntPtr
        Dim NewActivehWnd As IntPtr

        TopMosthWnd = GetWindow(Me.Handle, GW_OWNER)
        TopMosthWnd = GetWindow(TopMosthWnd, GW_HWNDFIRST)

        OldActivehWnd = SetActiveWindow(TopMosthWnd)
        NewActivehWnd = GetActiveWindow()

        SendKeys.SendWait("%f")
  End Sub

And

Code:

    Public Shared Sub AltPlusAlphaNumeric(ByVal Value As String)
          keybd_event(VK_MENU, _
                    MapVirtualKey(VK_MENU, 0),
                    0, 0)                          ' Press Alt
        keybd_event(&H46, _
                    MapVirtualKey(&H46, 0), _
                    0, 0)                          ' Press Key

        keybd_event(&H46, _
                    MapVirtualKey(&H46, 0), _
                    KEYEVENTF_KEYUP, 0)            ' Release Key

        keybd_event(VK_MENU, _
                    MapVirtualKey(VK_MENU, 0), _
                    KEYEVENTF_KEYUP, 0)            ' Release Alt
    End Sub

I am missing something and I am not sure what. I am fairly new to VB.NET.
Also is it better to use SendKeys or keybd_event?

Thanks in advance for any help

Jerry B

Viewing all articles
Browse latest Browse all 27382

Trending Articles