0

I want to send "{TAB}" Key to another application window(send the key to the window not to textbox).

I tried:

SendMessage(hWnd, WM_SETHOTKEY, VK_TAB, 0)

Nothing happened.
my goal is:
send tab key to my application Or other application when the application window is not in focus.
(i know that sendkey is not professional in this case there is no choice(This is the first time that I'm using it).)

I made many attempts and I always returned to the same result:

Nothing happened.

Does anyone know the answer?

Nmmmm
  • 187
  • 1
  • 2
  • 7

3 Answers3

8

SendKeys requires the application that you are sending the Keys to, to be active.

From above Link:

Use SendKeys to send keystrokes and keystroke combinations to the active application.

I order to get around this limitation you will have to resort to using the WinApi Functions.

  1. FindWindow pInvoke.net
  2. FindWindowEx pInvoke.net
  3. sendMessage pInvoke.net

See this MSDN Forum Post for an example

Here is a modified example from that Posting:

Public Class Form1
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                     (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
                     (ByVal hWnd As IntPtr, ByVal hWndChildAfterA As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                     (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
    Const WM_SETTEXT As Integer = &HC

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim destination As IntPtr = FindWindow(Nothing, "Untitled - Notepad")
        Dim destControl As IntPtr = FindWindowEx(destination, IntPtr.Zero, "Edit", Nothing)
        SendMessage(destControl, WM_SETTEXT, IntPtr.Zero, "Hello" & vbTab & "GoodBye" & vbCrLf)

    End Sub

End Class

Added an Additional Example using WM_KEYDOWN I created another small application with the Window Title set to TestForm and overrode the WndProc Method to determine if the application got the TabKey.

Sending Form

Public Class Form1

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                 (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                 (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
    Const WM_KEYDOWN As Integer = &H100
    Const VK_TAB As Integer = &H9

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim destination As IntPtr = FindWindow(Nothing, "TestForm")
        SendMessage(destination, WM_KEYDOWN, VK_TAB, 0)

    End Sub

End Class

Test Form

Put a breakpoint on MyBase.WndProc(m) and look at m to see what has been sent.

Public Class Form1

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
    End Sub

End Class
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • you sending to "Edit" class, i want to sendMessage(TAB KEY) to main window of -non- active aapplication. im using WM_KEYDOWN and WM_KEYUP and not WM_SETTEXT. – Nmmmm Oct 31 '12 at 09:22
  • @Nmmmm What exactly are you trying to accomplish, the example that you show in your question looks like you are trying to create a HotKey, though the documentation states that vk_Tab is invalid for that purpose. The code that I gave you will send text to the edit control that is part of Notepad. I will add another example to show WM_keyDown working. – Mark Hall Nov 01 '12 at 00:58
1

Having struggled with this type of this a few times before, i would suggest a couple of things to look at.

The 1st is autoit which includes a dll you can reference from vb.net, and is very simple you use, and well documented. I tend to use that whenever i need to control a 3rd party program.

The other is the ui automation classes See this for an example:

http://blog.functionalfun.net/2009/06/introduction-to-ui-automation-with.html

Steve
  • 20,703
  • 5
  • 41
  • 67
0

you need make the other window active first. check Change focus to another window in VB.NET . then use send key.

Community
  • 1
  • 1
urlreader
  • 6,319
  • 7
  • 57
  • 91
  • read what I wrote again. i want to send keys when the window is not in focus. – Nmmmm Oct 30 '12 at 22:39
  • 1
    I think you can not sendkeys that if it is not in focus. I'm 50% sure of this :) because I tried it some years ago, and could not figure out how to do it except bring the window to focus first. however, I could be totally wrong. – urlreader Oct 30 '12 at 23:06