永发信息网

VB后台按键

答案:4  悬赏:30  手机版
解决时间 2021-03-20 10:26
VB如何实现后台按键 (F4)
发个完整的编码来,谢谢

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

Private Sub Timer1_Timer()
Dim hwnd As Long ' 储存 FindWindow 函数返回的句柄
hwnd = FindWindow(vbNullString, "YB_OnlineClient") ' 取得进程标识符
If hwnd = 0 Then
Label1.Caption = "未运行"

Else
Label1.Caption = "已运行"
Text1.Text = hwnd
PostMessage hwnd, WM_KEYDOWN, VK_F4, 0& '按下F4键
PostMessage hwnd, WM_UP, VK_F4, 0& '释放F4键
End If
End Sub
我的代码有什么毛病吗怎么后台运行还是不行游戏没反应
最佳答案
算了,发个完整的代码给你吧,自己改.记得常数要定义.
当然,PostMessage的最后一个参数用0也可以,下面是完整的.

窗体代码:
Private Sub Form_Load()
Dim hwnd As Long
hwnd = &HXXXX 'XXXXX表示记事本编辑框的句柄
PostMessage hwnd, WM_KEYDOWN, VK_A, MakeKeyLparam(VK_A, WM_KEYDOWN) '按下A键
PostMessage hwnd, WM_CHAR, Asc("A"), MakeKeyLparam(VK_A, WM_KEYDOWN) '输入字符A
PostMessage hwnd, WM_UP, VK_A, MakeKeyLparam(VK_A, WM_UP) '释放A键
End Sub
模块代码:
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_CHAR = &H102
Public Const VK_A = &H41

Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
Dim s As String
Dim Firstbyte As String 'lparam参数的24-31位
If flag = WM_KEYDOWN Then '如果是按下键
Firstbyte = "00"
Else
Firstbyte = "C0" '如果是释放键
End If
Dim Scancode As Long
'获得键的扫描码
Scancode = MapVirtualKey(VirtualKey, 0)
Dim Secondbyte As String 'lparam参数的16-23位,即虚拟键扫描码
Secondbyte = Right("00" & Hex(Scancode), 2)
s = Firstbyte & Secondbyte & "0001" '0001为lparam参数的0-15位,即发送次数和其它扩展信息
MakeKeyLparam = Val("&H" & s)
End Function
全部回答
你的按下f1 postmessage hwnd, wm_keydown, vbkeyf1, 0& '按下f1键 postmessage hwnd, wm_up, vbkeyf1, 0& '释放f1键 按下f2 postmessage hwnd, wm_keydown, vbkeyf2, 0& '按下f2键 postmessage hwnd, wm_up, vbkeyf2, 0& '释放f2键 组合键ctrl + a postmessage hwnd, wm_keydown, vbkeycontrol, 0& '按下ctrl键 postmessage hwnd, wm_keydown, vbkeya, 0& '按下a键 postmessage hwnd, wm_up, vbkeya, 0& '释放a键 postmessage hwnd, wm_up, vbkeycontrol, 0& '释放ctrl键 我写个过程给你 private sub postkey(hwnd as long, keycode as keycodeconstants) postmessage hwnd, wm_keydown, keycode, 0& postmessage hwnd, wm_keyup, keycode, 0& end sub 使用的时候他会出现自动完成的,“vbkey”后面的内容就是你要的按的键
常量声明了吗?timer1.enabeld=true timer1.interval=100有吗?
你说的后台 是 类似qq那种 c+a+z 吧 这东西要hook 用api写的 代码 可以自己搜索 不过一般都有小问题
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
感谢上司请我们一起聚餐的语句
崩坏三刚正是什么意思
java 枚举可以大量用吗
极品飞车18 pc如何三连屏
新挖根雕树根怎样防止开裂
赞美禅的短文(80字左右)
谁见过河北大学医学部这么垃圾的老师???
为什么透过火焰看到火焰边缘的景象是波纹状的
OUSSKO香氛物语恒宝广场店地址有知道的么?有
汽车救援用吊车吊车在哪个位置最好
意木轩木门是中国木门30强吗?
日本东京SAT考场国际基督教大学好吗
同一只脚 能看出是正常足还是高足弓吗?
探究凸透镜成像的规律中 光屏的作用是什么
松树弯地址在哪,我要去那里办事
推荐资讯
介绍人先介绍职位高的还是低的
送假名和振假名是区别?
请问姜堰五金机电市场在哪?
足球服如何与鞋子搭配
木凳子的做法
是谁带来远古的呼唤 是谁留下千年的祈盼歌词
伊春市金山屯区盐务局地址在什么地方,想过去
终极一班3第八集汪东城唱的那首歌曲是
125×25×3.2
要是刚开始做生意,女装、女鞋、童装里应该选
嘉兴防火卷帘门安装哪家好
成昌大厦我想知道这个在什么地方
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?