你好我测试我的项目,它在桌面上工作得很好,甚至在游戏聊天中,但在游戏环境中,不工作..
{{{{KeyboardHook}}} :(((Class)))
Imports System.Runtime.InteropServices
Public Class KeyboardHook
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Private Overloads Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal HookProc As KBDLLHookProc, ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Private Overloads Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Private Overloads Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
End Function
<StructLayout(LayoutKind.Sequential)>
Private Structure KBDLLHOOKSTRUCT
Public vkCode As UInt32
Public scanCode As UInt32
Public flags As KBDLLHOOKSTRUCTFlags
Public time As UInt32
Public dwExtraInfo As UIntPtr
End Structure
<Flags()>
Private Enum KBDLLHOOKSTRUCTFlags As UInt32
LLKHF_EXTENDED = &H1
LLKHF_INJECTED = &H10
LLKHF_ALTDOWN = &H20
LLKHF_UP = &H80
End Enum
Public Shared Event KeyDown(ByVal Key As Keys)
Public Shared Event KeyUp(ByVal Key As Keys)
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const HC_ACTION As Integer = 0
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_SYSKEYUP = &H105
Private Delegate Function KBDLLHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
Private KBDLLHookProcDelegate As KBDLLHookProc = New KBDLLHookProc(AddressOf KeyboardProc)
Private HHookID As IntPtr = IntPtr.Zero
Private Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
If (nCode = HC_ACTION) Then
Dim struct As KBDLLHOOKSTRUCT
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
RaiseEvent KeyDown(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
Case WM_KEYUP, WM_SYSKEYUP
RaiseEvent KeyUp(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
End Select
End If
Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
End Function
Public Sub New()
HHookID = SetWindowsHookEx(WH_KEYBOARD_LL, KBDLLHookProcDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
If HHookID = IntPtr.Zero Then
Throw New Exception("Could not set keyboard hook")
End If
End Sub
Protected Overrides Sub Finalize()
If Not HHookID = IntPtr.Zero Then
UnhookWindowsHookEx(HHookID)
End If
MyBase.Finalize()
End Sub
End Class
- = - = - = - = - = - =
{{{Form1}}} :(((Vb)))
Public Class Form1
Private WithEvents kbHook As New KeyboardHook
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
Debug.WriteLine(Key.ToString)
If Key.ToString() = "G" Then
SendKeys.Send("R")
End If
If Key.ToString() = "J" Then
SendKeys.Send(" ")
End If
End Sub
Private Sub kbHook_KeyUp(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyUp
Debug.WriteLine(Key)
End Sub
End Class
我希望得到一些帮助,我已经测试过代码和工作,但没有进入游戏事件的
所以当我点击(g)键时,将使用(r)按键重新加载,当我点击(j)键时会点击SpaceBar键进入游戏。这一切,我没有做任何作弊,它只是为了获得一些知识。
答案 0 :(得分:0)
SendKeys
并不总是适用于游戏。尝试使用名为 InputHelper 的库(Download from GitHub)。它有两种不同的执行输入模拟的方法,包括一个易于使用的键盘钩。
请确保您在应用程序中或其中的某处包含其版权许可证,以便于用户找到它。
Imports InputHelperLib
Public Class Form1
Dim WithEvents KeyboardHook As New InputHelper.Hooks.KeyboardHook
Private Sub KeyboardHook_KeyDown(sender As Object, e As InputHelperLib.InputHelper.Hooks.KeyboardHookEventArgs) Handles KeyboardHook.KeyDown
Select Case e.KeyCode
Case Keys.G
InputHelper.Keyboard.PressKey(Keys.R)
Case Keys.J
InputHelper.Keyboard.PressKey(Keys.Space)
End Select
End Sub
End Class
如果这不起作用,可以尝试使用另外两种方法发送按键。
<强>或者:强>
Select Case e.KeyCode
Case Keys.G
InputHelper.Keyboard.PressKey(Keys.R, True)
Case Keys.J
InputHelper.Keyboard.PressKey(Keys.Space, True)
End Select
或强>
Select Case e.KeyCode
Case Keys.G
InputHelper.WindowMessages.SendKeyPress(Keys.R)
Case Keys.J
InputHelper.WindowMessages.SendKeyPress(Keys.Space)
End Select
我还没有记录库的其余部分,但在我的项目Wiki上,您可以找到有关钩子如何工作的信息。
所有方法都添加了XML文档,因此Visual Studio的IntelliSense将向您展示它们及其参数的作用。您也可以在VS中按 F12 并在对象浏览器中查看所有InputHelper的成员。
请注意,某些游戏只接受原始硬件输入(即直接从键盘/鼠标读取)。如果您正在尝试使用此游戏的情况,那么您找到的大多数解决方案将无效。
模拟输入的常规方法是将其附加到系统的鼠标和键盘输入流,或者将按键消息(窗口消息)发送到游戏窗口。但是,如果游戏只直接从输入设备获取原始输入,游戏将忽略这些。
你可以尝试谷歌搜索“DirectX输入模拟”或“DirectInput输入模拟”,但我怀疑你会发现任何有用的东西。