帮忙写个VB后台模拟按键的代码!

此后台按键用于网络游戏窗体里设两个按钮一个是运行,一个是暂停必须是后台按键,每按一下按键延时3秒(1,2,3,4,F1,F2都可),一直循环直到按暂停键为止.额,如果可能... 此后台按键用于网络游戏
窗体里设两个按钮
一个是运行,一个是暂停

必须是后台按键,每按一下按键延时3秒(1,2,3,4,F1,F2都可),一直循环直到按暂停键为止.

额,如果可能的话,两个按钮给搞下全局快捷键,开始F10,暂停F12.

纠结了一晚上没搞好,哎.
展开
 我来答
zgwxm
2011-09-16 · 经典歌曲、名人名家歌曲视频
zgwxm
采纳数:1792 获赞数:21142

向TA提问 私信TA
展开全部
下面代码按下F1则每3秒自动按下一次1键,按下F2则自动按键停止,按下F3程序退出。
不过,因为F1、F2还是系统的其它热键,所以,当你按下F1后,可能有帮助窗口弹出……
窗体代码:
Option Explicit
Private Sub Form_Load()
Form1.Visible = False
Timer1.Interval = 3000
Timer1.Enabled = False
HooK
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHooK
End Sub
Private Sub Timer1_Timer()
SendKeys "1"
End Sub
模块代码:
Option Explicit
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const VK_F1 = &H70
Private Const VK_F2 = &H71
Private Const VK_F3 = &H72
Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13
Private lngHook As Long
Public Sub LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long)
Dim blnNum As Integer
Dim p As PKBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
Select Case wParam
Case WM_KEYUP, WM_SYSKEYUP
Call CopyMemory(p, ByVal lParam, Len(p))
If p.vkCode = VK_F1 Then
blnNum = 1
ElseIf p.vkCode = VK_F2 Then
blnNum = 2
ElseIf p.vkCode = VK_F3 Then
blnNum = 3
Else
blnNum = 0
End If
Case Else
End Select
End If
If blnNum = 1 Then
Form1.Timer1.Enabled = True
ElseIf blnNum = 2 Then
Form1.Timer1.Enabled = False
ElseIf blnNum = 3 Then
UnHooK
End
Else
Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)
End If
End Sub
Public Sub HooK()
lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0)
End Sub
Public Sub UnHooK()
Call UnhookWindowsHookEx(lngHook)
End Sub
更多VB代码可参阅我的博客:http://hi.baidu.com/zgmg/blog/item/8ea6aab18339234f092302db.html
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
zx001z7d53
2011-09-16 · TA获得超过2万个赞
知道大有可为答主
回答量:2.4万
采纳率:52%
帮助的人:5786万
展开全部
需要找到句柄,给你个参考实例
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal Hwnd1 As Long, ByVal Hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Form_Load()
Shell "calc.exe", vbNormalNoFocus
End Sub

Private Sub Command1_Click()
Dim Hwnd1 As Long
Dim Hwnd2 As Long
Hwnd1 = FindWindow("SciCalc", "计算器")
Hwnd2 = FindWindowEx(Hwnd1, 0, "Button", "1")
SendMessage Hwnd2, WM_LBUTTONDOWN, ByVal 0&, ByVal 0&
SendMessage Hwnd2, WM_LBUTTONUP, ByVal 0&, ByVal 0&
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
尼欧赵
2011-09-16 · TA获得超过823个赞
知道大有可为答主
回答量:1597
采纳率:0%
帮助的人:1221万
展开全部
用按键精灵吧,现成的,不用自己写程序。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式