关于VB中定义全局热键

PrivateDeclareFunctionFindWindowLib"user32"Alias"FindWindowA"(ByVallpClassNameAsStrin... 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 Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Dim DRHwnd As Long
Dim DRPid As Long
Dim DRProcessHandle As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Sub 启动_Click()
DRHwnd = FindWindow("ABCD_CLASS", "abcd")
GetWindowThreadProcessId DRHwnd, DRPid
DRProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, DRPid)
If DRProcessHandle = 0 Then
MsgBox "启动失败", , "S版"
Else
MsgBox "辅助工具"
Timer1.Enabled = True
End If
End Sub

Private Sub 地址写入_Click()
WriteProcessMemory DRProcessHandle, &H5E23E8, &H0, 1, 0&
WriteProcessMemory DRProcessHandle, &H5EE1CEC, &H1, 1, 0&
End Sub
----------------------------
如何在任何窗口下按F2激活窗体代码“地址写入”,我现在只能在窗口中用鼠标点击按钮来激活,麻烦哪位老大帮我补一下代码。
非常感谢!如果是组合键怎么写好?比如Ctrl+D之类的。
If GetAsyncKeyState(vbKeyControl) And GetAsyncKeyState(vbKeyD) Then
是这样吧?
展开
 我来答
ljl88900
2010-01-31 · TA获得超过2661个赞
知道大有可为答主
回答量:2197
采纳率:100%
帮助的人:2625万
展开全部
在窗体上加入控件Timer1(Interval=100),然后复制下面代码,即可实现上述功能:

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer '增加的代码
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 Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Dim DRHwnd As Long
Dim DRPid As Long
Dim DRProcessHandle As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Sub 启动_Click()
DRHwnd = FindWindow("ABCD_CLASS", "abcd")
GetWindowThreadProcessId DRHwnd, DRPid
DRProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, DRPid)
If DRProcessHandle = 0 Then
MsgBox "启动失败", , "S版"
Else
MsgBox "辅助工具"
Timer1.Enabled = True
End If
End Sub

Private Sub 地址写入_Click()
WriteProcessMemory DRProcessHandle, &H5E23E8, &H0, 1, 0&
WriteProcessMemory DRProcessHandle, &H5EE1CEC, &H1, 1, 0&
End Sub

Private Sub Timer1_Timer() '增加的代码
If GetAsyncKeyState(vbKeyF2) Then 地址写入_Click
End Sub
伯镶柏7k
2010-01-31 · TA获得超过2768个赞
知道小有建树答主
回答量:754
采纳率:0%
帮助的人:0
展开全部
Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer

Timer循环检测就行了。。
参数使用VB自带的vbKey系列参数,如vbKeyF2
返回值不为零是说明此键被按下。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
二白Q0
2010-01-31 · TA获得超过2413个赞
知道大有可为答主
回答量:990
采纳率:0%
帮助的人:1091万
展开全部
窗体代码:
'--------------------------------------------------
Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)

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 Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Dim DRHwnd As Long
Dim DRPid As Long
Dim DRProcessHandle As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Dim nAtom As Long

Private Sub Form_Load(
'将F2注册为系统热键
nAtom = GlobalAddAtom("myProgram")
RegisterHotKey Me.hWnd, nAtom, ByVal 0&, vbKeyF2
lOldProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnregisterHotKey hWnd, nAtom
GlobalDeleteAtom nAtom
SetWindowLong Me.hWnd, GWL_WNDPROC, lOldProc
End Sub

Private Sub 启动_Click()
DRHwnd = FindWindow("ABCD_CLASS", "abcd")
GetWindowThreadProcessId DRHwnd, DRPid
DRProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, DRPid)
If DRProcessHandle = 0 Then
MsgBox "启动失败", , "S版"
Else
MsgBox "辅助工具"
Timer1.Enabled = True
End If
End Sub

Private Sub 地址写入_Click()
WriteProcessMemory DRProcessHandle, &H5E23E8, &H0, 1, 0&
WriteProcessMemory DRProcessHandle, &H5EE1CEC, &H1, 1, 0&
End Sub
'--------------------------------------------------

模块代码:
'--------------------------------------------------
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_HOTKEY = &H312

Public lOldProc As Long

Public Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case Msg
Case WM_HOTKEY
If (lParam And &HFFFF0000) / &H10000 = vbKeyF2 Then
call form1.地址写入_Click() '注意将这里Form1改为实际的窗体名称
End If
End Select
WndProc = CallWindowProc(lOldProc, hWnd, Msg, wParam, lParam)
End Function
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
fjnp007
2010-01-31 · TA获得超过396个赞
知道答主
回答量:200
采纳率:0%
帮助的人:0
展开全部
在窗体上加入一个Timer1控件,Interval为100,然后复制下面代码,即可实现功能:

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
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 Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Dim DRHwnd As Long
Dim DRPid As Long
Dim DRProcessHandle As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Sub 启动_Click()
DRHwnd = FindWindow("ABCD_CLASS", "abcd")
GetWindowThreadProcessId DRHwnd, DRPid
DRProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, DRPid)
If DRProcessHandle = 0 Then
MsgBox "启动失败", , "S版"
Else
MsgBox "辅助工具"
Timer1.Enabled = True
End If
End Sub

Private Sub 地址写入_Click()
WriteProcessMemory DRProcessHandle, &H5E23E8, &H0, 1, 0&
WriteProcessMemory DRProcessHandle, &H5EE1CEC, &H1, 1, 0&
End Sub

Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF2) Then 地址写入_Click
End Sub

'end
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
aspd199
2010-02-01 · TA获得超过906个赞
知道小有建树答主
回答量:865
采纳率:50%
帮助的人:484万
展开全部
定义全局热键不用搞这么复杂吧
做一个hook函数 检测到F2就call代码完事了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友28f85b68d86
2019-06-16 · TA获得超过3911个赞
知道大有可为答主
回答量:3266
采纳率:30%
帮助的人:229万
展开全部
将注册热键时的Modifiers和uVirtKey1这两个变量赋值为其他值即可。
可以用textbox控件的KeyDown事件获取Keycode值和Shift值,替换uVirtKey1和Modifiers的数值。
值得注意的是KeyDown里的Shift值和Modifiers的值顺序正好相反。
KeyDown里Alt为4,Ctrl为2,Shift为1
而RegisterHotKey里的Modifiers值Alt为1,Ctrl为2,Shift为4。
vbKeyQ其实是一个常数值,它的值就是用KeyDown事件获取的Keycode值。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式