我刚才写了一组发送键功能。它在32位系统中工作。现在我再次测试它并意识到它不再适用于我的新win7 64位系统。 我在网上找到了一些文章,然后按their way来创建Input32 / 64函数。但它似乎不起作用。
Dim result As Integer = SendInput(1, input, cbSize)
始终返回0并且没有模拟击键。你能看一下吗?
(注意:我只做了KEYBOARDINPUT64结构,它不起作用,所以我没有制作MOUSEINPUT64或HARDWAREINPUT64结构......)
''mouse functions
Private Shared Sub SendMouseInput(ByVal flags As MOUSEEVENTF, ByVal point As System.Drawing.Point)
If Marshal.SizeOf(New IntPtr()) = 8 Then
SendMouseInput64(flags, point)
Else
SendMouseInput32(flags, point)
End If
End Sub
Private Shared Sub SendMouseInput64(ByVal flags As MOUSEEVENTF, ByVal point As System.Drawing.Point)
Dim screen As Rectangle = My.Computer.Screen.Bounds
Dim globalx As Integer = CInt((65535 * point.X) / screen.Width) ' x scale from 0 to 65535
Dim globaly As Integer = CInt((65535 * point.Y) / screen.Height) ' y scale from 0 to 65535
Dim input As New INPUT64
input.dwType = InputType.Mouse
input.mi.dx = globalx
input.mi.dy = globaly
input.mi.dwFlags = MOUSEEVENTF.ABSOLUTE Or flags
input.mi.dwExtraInfo = IntPtr.Zero
input.mi.mouseData = 0
input.mi.time = 0
SendInput(1, input, Marshal.SizeOf(input))
End Sub
Private Shared Sub SendMouseInput32(ByVal flags As MOUSEEVENTF, ByVal point As System.Drawing.Point)
Dim screen As Rectangle = My.Computer.Screen.Bounds
Dim globalx As Integer = CInt((65535 * point.X) / screen.Width) ' x scale from 0 to 65535
Dim globaly As Integer = CInt((65535 * point.Y) / screen.Height) ' y scale from 0 to 65535
Dim input As New INPUT32
input.dwType = InputType.Mouse
input.mi.dx = globalx
input.mi.dy = globaly
input.mi.dwFlags = MOUSEEVENTF.ABSOLUTE Or flags
input.mi.dwExtraInfo = IntPtr.Zero
input.mi.mouseData = 0
input.mi.time = 0
SendInput(1, input, Marshal.SizeOf(input))
End Sub
''keyboard functions
Private Shared Sub SendKeyInput(ByVal flags As KEYEVENTF, ByVal key As Int16)
If Marshal.SizeOf(New IntPtr()) = 8 Then
SendKeyInput64(flags, key)
Else
SendKeyInput32(flags, key)
End If
End Sub
Private Shared Sub SendKeyInput64(ByVal flags As KEYEVENTF, ByVal key As Int16)
Dim input As New INPUT64
Dim ki As New KEYBDINPUT64
input.dwType = InputType.Keyboard
input.ki = ki
input.ki.wVk = key
input.ki.wScan = 0
input.ki.time = 0
input.ki.dwFlags = flags
input.ki.dwExtraInfo = IntPtr.Zero
Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT64))
Dim result As Integer = SendInput(1, input, cbSize)
If result = 0 Then Console.WriteLine("SendKeyInput:" & Marshal.GetLastWin32Error)
End Sub
Private Shared Sub SendKeyInput32(ByVal flags As KEYEVENTF, ByVal key As Int16)
Dim input As New INPUT32
Dim ki As New KEYBDINPUT32
input.dwType = InputType.Keyboard
input.ki = ki
input.ki.wVk = key
input.ki.wScan = 0
input.ki.time = 0
input.ki.dwFlags = flags
input.ki.dwExtraInfo = IntPtr.Zero
Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT32))
Dim result As Integer = SendInput(1, input, cbSize)
If result = 0 Then Messaging.Instance.ReportError("MouseKeyboard::SendKeyInput:" & Marshal.GetLastWin32Error)
End Sub
''lower level input functions
<DllImport("User32.dll", SetLastError:=True)> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT64, ByVal cbSize As Integer) As Integer
End Function
<DllImport("User32.dll", SetLastError:=True)> _
Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT32, ByVal cbSize As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Private Shared Function BlockInput(ByVal fBlockIt As Boolean) As Boolean
End Function
<StructLayout(LayoutKind.Explicit, pack:=1)> _
Private Structure INPUT64
<FieldOffset(0)> Public dwType As InputType
<FieldOffset(8)> Public mi As MOUSEINPUT
<FieldOffset(8)> Public ki As KEYBDINPUT64
<FieldOffset(8)> Public hi As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Explicit, pack:=1)> _
Private Structure INPUT32
<FieldOffset(0)> Public dwType As InputType
<FieldOffset(4)> Public mi As MOUSEINPUT
<FieldOffset(4)> Public ki As KEYBDINPUT32
<FieldOffset(4)> Public hi As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure MOUSEINPUT
Public dx As Int32
Public dy As Int32
Public mouseData As Int32
Public dwFlags As MOUSEEVENTF
Public time As Int32
Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure KEYBDINPUT64
<FieldOffset(0)> Public wVk As Int16
<FieldOffset(2)> Public wScan As Int16
<FieldOffset(4)> Public dwFlags As KEYEVENTF
<FieldOffset(12)> Public time As Int32
<FieldOffset(16)> Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure KEYBDINPUT32
Public wVk As Int16
Public wScan As Int16
Public dwFlags As KEYEVENTF
Public time As Int32
Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure HARDWAREINPUT
Public uMsg As Int32
Public wParamL As Int16
Public wParamH As Int16
End Structure
Private Enum InputType As Integer
Mouse = 0
Keyboard = 1
Hardware = 2
End Enum
<Flags()> _
Private Enum MOUSEEVENTF As Integer
MOVE = &H1
LEFTDOWN = &H2
LEFTUP = &H4
RIGHTDOWN = &H8
RIGHTUP = &H10
MIDDLEDOWN = &H20
MIDDLEUP = &H40
XDOWN = &H80
XUP = &H100
VIRTUALDESK = &H400
WHEEL = &H800
ABSOLUTE = &H8000
End Enum
<Flags()> _
Private Enum KEYEVENTF As Integer
KEYDOWN = 0
EXTENDEDKEY = 1
KEYUP = 2
[UNICODE] = 4
SCANCODE = 8
End Enum
答案 0 :(得分:2)
与此问题的解决方案不同,但您是否尝试过InputSimulator库:https://inputsimulator.codeplex.com/
我已经在32/64位版本的Windows上使用它而没有任何障碍。
答案 1 :(得分:1)
请看看这个。 C# PInvoking user32.dll on a 64 bit system
我想这也适用于其他user32.dll调用...
来源http://www.pinvoke.net/default.aspx/user32.SendMessage
如果你处于紧张状态...... SendInput Keys in Win32 & Win64 machines