VB 全局热键

要求:按下HOME键,程序就执行一段代码,我要在激活其他程序的时候也能用热键运行的方法!最好能把一步一步的注释写出来。我是要在ActiveX控件里按下HOME键则执行后面... 要求:按下HOME键,程序就执行一段代码,我要在激活其他程序的时候也能用热键运行的方法! 最好能把一步一步的注释写出来。
我是要在ActiveX控件里按下HOME键则执行后面的代码,越详细越好,回答正确的追加100分
展开
 我来答
红靖皋又蓝
2019-06-06 · TA获得超过3815个赞
知道大有可为答主
回答量:3051
采纳率:31%
帮助的人:376万
展开全部
主窗体代码如下:
Option
Explicit
'程序启动时注册功能热键F12
Private
Sub
Form_Load()
Dim
Modifiers
As
Long
preWinProc
=
GetWindowLong(Me.hWnd,
GWL_WNDPROC)
SetWindowLong
Me.hWnd,
GWL_WNDPROC,
AddressOf
WndProc
uVirtKey
=
vbKeyDelete
RegisterHotKey
Me.hWnd,
1,
Modifiers,
uVirtKey
End
Sub
'当程序被关闭时,取消已经注册的热键
Private
Sub
Form_Unload(Cancel
As
Integer)
SetWindowLong
Me.hWnd,
GWL_WNDPROC,
preWinProc
UnregisterHotKey
Me.hWnd,
uVirtKey
'取消系统级热键,释放资源
End
'终止程序运行
End
Sub
添加一个标准模块,代码如下
Option
Explicit
'在窗口结构中为指定的窗口设置信息
Public
Declare
Function
SetWindowLong
Lib
"user32"
Alias
"SetWindowLongA"
(ByVal
hWnd
As
Long,
ByVal
nIndex
As
Long,
ByVal
dwNewLong
As
Long)
As
Long
'从指定窗口的结构中取得信息
Public
Declare
Function
GetWindowLong
Lib
"user32"
Alias
"GetWindowLongA"
(ByVal
hWnd
As
Long,
ByVal
nIndex
As
Long)
As
Long
'运行指定的进程
Public
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
'向系统注册一个指定的热键
Public
Declare
Function
RegisterHotKey
Lib
"user32"
(ByVal
hWnd
As
Long,
ByVal
ID
As
Long,
ByVal
fsModifiers
As
Long,
ByVal
vk
As
Long)
As
Long
'取消热键并释放占用的资源
Public
Declare
Function
UnregisterHotKey
Lib
"user32"
(ByVal
hWnd
As
Long,
ByVal
ID
As
Long)
As
Long
'上述五个API函数是注册系统级热键所必需的,具体实现过程如后文所示
'热键标志常数,用来判断当键盘按键被按下时是否命中了我们设定的热键
Public
Const
WM_HOTKEY
=
&H312
Public
Const
GWL_WNDPROC
=
(-4)
'定义系统的热键,原中断标示,被隐藏的项目句柄
Public
preWinProc
As
Long,
MyhWnd
As
Long,
uVirtKey
As
Long
'热键拦截过程
Public
Function
WndProc(ByVal
hWnd
As
Long,
ByVal
Msg
As
Long,
ByVal
wParam
As
Long,
ByVal
lParam
As
Long)
As
Long
If
Msg
=
WM_HOTKEY
Then
'如果拦截到热键标志常数
If
wParam
=
1
Then
'如果是我们的定义的热键...
HideDone
'执行隐藏鼠标所指项目
End
If
End
If
'如果不是热键,或者不是我们设置的热键,交还控制权给系统,继续监测热键
WndProc
=
CallWindowProc(preWinProc,
hWnd,
Msg,
wParam,
lParam)
End
Function
'最关键的项目隐藏过程
Public
Sub
HideDone()
MsgBox
"按下了Delete键"
End
Sub
宗印枝风缎
2019-08-29 · TA获得超过3.7万个赞
知道大有可为答主
回答量:1.2万
采纳率:27%
帮助的人:984万
展开全部
VB本身没有全局热键一说,可以调用API获取键盘消息,否则只能对所有的对象分别写on_keypress。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hxf0663
2009-02-25 · TA获得超过909个赞
知道小有建树答主
回答量:1374
采纳率:0%
帮助的人:979万
展开全部
新建一个模块,复制以下代码:
Option Explicit

Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
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
Declare Function RegisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long) As Long

Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)

Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long

Private Type taLong
ll As Long
End Type

Private Type t2Int
lWord As Integer
hWord As Integer
End Type

Public Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
'全局热键执行事件,如:
msgbox "30分?"
End If
End If
End If
Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function

在窗体中添加复制以下代码:
Private Sub Form_Load()
Dim ret As Long
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf Wndproc)
idHotKey = 1
‘如果全局热键alt+home,则加:'Modifiers = MOD_ALT
uVirtKey = vbKeyHome '全局热键:Home
ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub

Private Sub Form_Unload(Cancel As Integer) ‘关闭程序前取消全局热键的注册
Dim ret As Long
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hwnd, uVirtKey)
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
djrm_yb
2009-02-25 · TA获得超过1927个赞
知道小有建树答主
回答量:1035
采纳率:100%
帮助的人:776万
展开全部
这里能找到你想要的,注释就自己写把,论坛上注册一下就可以下载了,有原代码

参考资料: http://www.vbgood.com/viewthread.php?tid=80271&extra=page%3D1

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
汉深吴大吧世活9428
2009-02-26
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
百度搜索 VB全局热键
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式