游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6976|回复: 7

SendInput这个API如何用.请给个例子

[复制链接]

11

主题

49

帖子

49

积分

注册会员

Rank: 2

积分
49
发表于 2006-9-26 20:04:00 | 显示全部楼层 |阅读模式
如题.想编个模拟鼠标事件.玩网游偷懒...结果那游戏貌似屏蔽sendmessage...就试试如题API了.谁有例子的给个..谢谢!

11

主题

49

帖子

49

积分

注册会员

Rank: 2

积分
49
 楼主| 发表于 2006-9-26 20:20:00 | 显示全部楼层

Re:SendInput这个API如何用.请给个例子

发现在VB的API里无法调用...请问怎么解决..

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
发表于 2006-9-27 14:29:00 | 显示全部楼层

Re:SendInput这个API如何用.请给个例子

Const VK_H = 72
Const VK_E = 69
Const VK_L = 76
Const VK_O = 79
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
  dx As Long
  dy As Long
  mouseData As Long
  dwFlags As Long
  time As Long
  dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
  wVk As Integer
  wScan As Integer
  dwFlags As Long
  time As Long
  dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
  uMsg As Long
  wParamL As Integer
  wParamH As Integer
End Type
Private Type GENERALINPUT
  dwType As Long
  xi(0 To 23) As Byte
End Type
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Form_KeyPress(KeyAscii As Integer)
    'Print the key on the form
    Me.Print Chr$(KeyAscii);
End Sub
Private Sub Form_Paint()
   'Clear the form
    Me.Cls
    'call the SendKey-function
    SendKey VK_H
    SendKey VK_E
    SendKey VK_L
    SendKey VK_L
    SendKey VK_O
End Sub
Private Sub SendKey(bKey As Byte)
    Dim GInput(0 To 1) As GENERALINPUT
    Dim KInput As KEYBDINPUT
    KInput.wVk = bKey  'the key we're going to press
    KInput.dwFlags = 0 'press the key
    'copy the structure into the input array's buffer.
    GInput(0).dwType = INPUT_KEYBOARD   ' keyboard input
    CopyMemory GInput(0).xi(0), KInput, Len(KInput)
    'do the same as above, but for releasing the key
    KInput.wVk = bKey  ' the key we're going to realease
    KInput.dwFlags = KEYEVENTF_KEYUP  ' release the key
    GInput(1).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory GInput(1).xi(0), KInput, Len(KInput)
    'send the input now
    Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
发表于 2006-9-27 14:32:00 | 显示全部楼层

还有几个相关的 keybd_event

Const VK_H = 72
Const VK_E = 69
Const VK_L = 76
Const VK_O = 79
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub Form_KeyPress(KeyAscii As Integer)
    'Print the key on the form
    Me.Print Chr$(KeyAscii);
End Sub
Private Sub Form_Paint()
    'Clear the form
    Me.Cls
    keybd_event VK_H, 0, 0, 0   ' press H
    keybd_event VK_H, 0, KEYEVENTF_KEYUP, 0   ' release H
    keybd_event VK_E, 0, 0, 0  ' press E
    keybd_event VK_E, 0, KEYEVENTF_KEYUP, 0  ' release E
    keybd_event VK_L, 0, 0, 0  ' press L
    keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0  ' release L
    keybd_event VK_L, 0, 0, 0  ' press L
    keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0  ' release L
    keybd_event VK_O, 0, 0, 0  ' press O
    keybd_event VK_O, 0, KEYEVENTF_KEYUP, 0  ' release O
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
发表于 2006-9-27 14:33:00 | 显示全部楼层

还有几个相关的 mouse_event

'Before you start this program, I suggest you save everything that wasn't saved yet.
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Form_Activate()
    Do
        'Simulate a mouseclick on the cursor's position
        mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, cButt, dwEI
        DoEvents
    Loop
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
发表于 2006-9-27 14:34:00 | 显示全部楼层

Re:SendInput这个API如何用.请给个例子

'Paste this code in a Form
'with a Menu named menu1 which has a menuitem named menu2

Private Type POINTAPI
    x As Long
    y As Long
End Type
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemRect Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long, ByVal uItem As Long, lprcItem As RECT) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
Private Const MOUSEEVENTF_MIDDLEUP = &H40
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Const SM_CXSCREEN = 0 'X Size of screen
Const SM_CYSCREEN = 1 'Y Size of Screen

Private Sub Form_KeyPress(KeyAscii As Integer)
    Dim mWnd As Long
    mWnd = Me.hwnd
   
    Dim hMenu As Long, hSubMenu As Long

    hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
    ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
    hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
    ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
   
End Sub


Private Sub ScreenToAbsolute(lpPoint As POINTAPI)
lpPoint.x = lpPoint.x * (&HFFFF& / GetSystemMetrics(SM_CXSCREEN))
lpPoint.y = lpPoint.y * (&HFFFF& / GetSystemMetrics(SM_CYSCREEN))
End Sub

Private Sub Click(p As POINTAPI)
'p.X and p.Y in absolute coordinates
'Put the mouse on the point

mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, p.x, p.y, 0, GetMessageExtraInfo()
'Mouse Down
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()
'Mouse Up
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()
End Sub

Private Sub ClickMenuItem(ByVal mWnd As Long, ByVal hMenu As Long, ByVal Pos As Long)
Dim ret As Long
Dim r As RECT, p As POINTAPI
ret = GetMenuItemRect(mWnd, hMenu, Pos, r)
If ret = 0 Then Exit Sub
p.x = (r.Left + r.Right) / 2
p.y = (r.Top + r.Bottom) / 2
ScreenToAbsolute p
'Click on p
Click p
End Sub

Private Sub Form_Load()
Dim mWnd As Long, p As POINTAPI
mWnd = Me.hwnd
Dim hMenu As Long, hSubMenu As Long
hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
p.x = &HFFFF& / 2
p.y = &HFFFF& / 2
Click p
Me.AutoRedraw = True
Me.BackColor = vbWhite
Print &quotress any key"
End Sub

Private Sub menu2_Click()
MsgBox "Click"
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
发表于 2006-9-27 14:35:00 | 显示全部楼层

Re:SendInput这个API如何用.请给个例子

就那么多了,相关的4个例子都给你了,不够就继续讨论~~~~~~

11

主题

49

帖子

49

积分

注册会员

Rank: 2

积分
49
 楼主| 发表于 2006-9-29 10:46:00 | 显示全部楼层

Re:SendInput这个API如何用.请给个例子

3ks谢谢了...继续受教中!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-25 12:44

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表