我正在使用SendInput将一些键盘命令发送到对话框。当我调用SendInput时,它已经是活动的前景窗口。
我首先发送“Return”然后发送其他一些东西。此对话框打开时默认选中一个按钮,当手动选择回车时,对话框上的按钮会进入下一个屏幕,但我的SendInput调用不会使对话框前进。
这是我的代码。首先是数据结构和p / invoke
Imports System.Runtime.InteropServices
Imports System.Drawing
Public Class NativeMethods
Private Structure INPUT
Dim dwType As Integer
Dim mkhi As MOUSEKEYBDHARDWAREINPUT
End Structure
Private Structure KEYBDINPUT
Public wVk As Short
Public wScan As Short
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
Private Structure HARDWAREINPUT
Public uMsg As Integer
Public wParamL As Short
Public wParamH As Short
End Structure
<StructLayout(LayoutKind.Explicit)> _
Private Structure MOUSEKEYBDHARDWAREINPUT
<FieldOffset(0)> Public mi As MOUSEINPUT
<FieldOffset(0)> Public ki As KEYBDINPUT
<FieldOffset(0)> Public hi As HARDWAREINPUT
End Structure
Private Structure MOUSEINPUT
Public dx As Integer
Public dy As Integer
Public mouseData As Integer
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As Integer ' changed from IntPtr because of compiler error.
' I don't use this field '
End Structure
' Constants for SendInput '
Const INPUT_MOUSE As UInt32 = 0
Public Const INPUT_KEYBOARD As Integer = 1
Const INPUT_HARDWARE As Integer = 2
Public Const KEYEVENTF_EXTENDEDKEY As UInt32 = &H1
Public Const KEYEVENTF_KEYUP As UInt32 = &H2
Public Const KEYEVENTF_UNICODE As UInt32 = &H4
Public Const KEYEVENTF_SCANCODE As UInt32 = &H8
Const XBUTTON1 As UInt32 = &H1
Const XBUTTON2 As UInt32 = &H2
Const MOUSEEVENTF_MOVE As UInt32 = &H1
Const MOUSEEVENTF_LEFTDOWN As UInt32 = &H2
Const MOUSEEVENTF_LEFTUP As UInt32 = &H4
Const MOUSEEVENTF_RIGHTDOWN As UInt32 = &H8
Const MOUSEEVENTF_RIGHTUP As UInt32 = &H10
Const MOUSEEVENTF_MIDDLEDOWN As UInt32 = &H20
Const MOUSEEVENTF_MIDDLEUP As UInt32 = &H40
Const MOUSEEVENTF_XDOWN As UInt32 = &H80
Const MOUSEEVENTF_XUP As UInt32 = &H100
Const MOUSEEVENTF_WHEEL As UInt32 = &H800
Const MOUSEEVENTF_VIRTUALDESK As UInt32 = &H4000
Const MOUSEEVENTF_ABSOLUTE As UInt32 = &H8000
Public Const VK_TAB = &H9
Public Const VK_RETURN = &HD
Public Const VK_DOWN = &H28
<DllImport("user32.dll")> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByClass( _
ByVal lpClassName As String, _
ByVal zero As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption( _
ByVal zero As IntPtr, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function SetFocus(ByVal hWnd As IntPtr) As IntPtr
End Function
' "As any" replacement seen here '
' when you see As Any in Pinvoke, replace with this '
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(<System.Runtime.InteropServices.MarshalAsAttribute( _
System.Runtime.InteropServices.UnmanagedType.AsAny)> _
ByVal pDst As Object, <System.Runtime.InteropServices.MarshalAsAttribute( _
System.Runtime.InteropServices.UnmanagedType.AsAny)> _
ByVal pSrc As Object, ByVal ByteLen As Long)
然后我的代码写入此对话框
' constants for keys needed '
Public Const VK_TAB = &H9
Public Const VK_RETURN = &HD
Public Const VK_DOWN = &H28
Public Shared Sub SendRebuildKeys()
' Enter '
SendKeyDownAndUp(VK_RETURN)
' Down x 10 '
For i = 1 To 10
SendKeyDownAndUp(VK_DOWN)
Next
' Enter '
SendKeyDownAndUp(VK_RETURN)
' T 84 '
SendKeyDownAndUp(CShort(84))
' S 83 '
SendKeyDownAndUp(CShort(83))
' T 84 '
SendKeyDownAndUp(CShort(84))
' Tab '
SendKeyDownAndUp(VK_TAB)
' Enter '
SendKeyDownAndUp(VK_RETURN)
End Sub
Public Shared Sub SendKeyDownAndUp(ByVal keyCode As Short)
Dim inputd = GetINPUTFromKeyCode(keyCode, True)
Dim inputu = GetINPUTFromKeyCode(keyCode, False)
Dim inputs(2) As INPUT
inputs(0) = inputd
inputs(1) = inputu
SendInput(2, inputs(0), Len(inputd))
End Sub
Private Shared Function GetINPUTFromKeyCode(ByVal keyCode As Short, _
ByVal isDown As Boolean) As INPUT
Dim input As New INPUT()
Dim unall As New MOUSEKEYBDHARDWAREINPUT()
Dim keyb As New KEYBDINPUT()
input.dwType = INPUT_KEYBOARD
keyb.wVk = keyCode
If isDown = False Then
keyb.dwFlags = KEYEVENTF_KEYUP
Else
keyb.dwFlags = 0
End If
' copy keyb into unall.ki '
CopyMemory(unall.ki, keyb, Len(keyb))
' copy unall into input.mkhi '
CopyMemory(input.mkhi, unall, Len(unall))
Return input
End Function
我在这里缺少什么
答案 0 :(得分:1)
问题可能在于编组您正在使用的数据结构。你能发帖吗?另外,发布P / Invoked函数的声明。
您是否已独立测试SendInput功能?它有用吗?如果你还没有这样做,我建议编写一个更简单的测试用例,以确保你的P / Invoked函数声明和你正在使用的数据结构没有问题。