How to programatically press Tab and enter key by vb.net coding

后端 未结 1 1459
离开以前
离开以前 2020-12-22 06:42

I am using a web browser control in which i am opening a website . On the third page of the website , there is a button . So after the loading of this 3rd page , I want to p

1条回答
  •  既然无缘
    2020-12-22 07:21

    First, you need to find the window handle you want to send the keys to, then send the actual keys you want. Here are the methods necessary to do this:

      'API calls
      Public Declare Function FindWindowExA Lib "user32.dll" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
      Public Declare Function SendMessageA Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    
      'Function to find window by either ClassName or WindowText
      Public Shared Function FindWindowEx(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As Int32, ByVal ClassName As String, ByVal WindowName As String) As IntPtr
         Return FindWindowExA(hWndParent, hWndChildAfter, ClassName, WindowName)
      End Function
    
      'Function to send pressed keys to specified window by handle
      Public Shared Function SendKeyToWindow(ByVal hwnd As IntPtr, ByVal data As String) As Boolean
    
         Try
            Dim rc As Int32
            Dim asciiChar As Byte
    
            If data.Length < 1 Then          'if no data
               Return True
            End If
    
            'If data.Length > 1 Then
            '    data = data.ToUpper
            'End If
    
            'sendMessageA is used to issue messages to windows simulating keypresses
            Select Case data.ToUpper
               Case Chr(13)
                  rc = SendMessageA(hwnd, WM_KEYDOWN, &HD, &H1C0001)                  'send Enter Key
                  rc = SendMessageA(hwnd, WM_CHAR, &HD, &H1C0001)                 'send Enter Key
                  rc = SendMessageA(hwnd, WM_KEYUP, &HBE, &HC0340001)                   'send Ener Key
               Case Chr(&H9)               'tab key
                  rc = SendMessageA(hwnd, WM_KEYDOWN, &H9, &HF0001)                  'send tab Key
                  rc = SendMessageA(hwnd, WM_KEYUP, &H9, &HC00F0001)                 'send tab Key
               Case Else
                  asciiChar = CByte(Asc(data.Substring(0, 1)))
                  rc = SendMessageA(hwnd, WM_CHAR, asciiChar, 0)                  'send 0 Key
            End Select
    
            Return True
         Catch ex As Exception
            Return False
         End Try
    
         Return False
    
      End Function
    

    In your instance, you likely just need to call the sendKeyToWindow function with the handle of the webbrowser controls:

    SendKeyToWindow(WebBrowser1.Handle, Chr(&H9)) SendKeyToWindow(WebBrowser1.Handle, Chr(13))

    Hope that helps.

    0 讨论(0)
提交回复
热议问题