vb定义多个键盘快捷键中的一个疑问

OptionExplicit'声明API函数PrivateDeclareFunctionRegisterHotKeyLib"user32"(ByValhWndAsLong... Option Explicit
' 声明API函数
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 PeekMessage Lib "user32 " Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32 " () As Long
' 声明结构
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
' 声明常数
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312

Private HotKey_Flg As Boolean

Private Sub Form_Load()
Dim Message As Msg
'注册 Ctrl+Shift+I和Ctrl+Shift+K 为热键
RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL + MOD_SHIFT, vbKeyI
RegisterHotKey Me.hWnd, &HBFFA&, MOD_CONTROL + MOD_SHIFT, vbKeyK
RegisterHotKey Me.hWnd, &HBFFA&, MOD_CONTROL + MOD_SHIFT, vbKeyH
RegisterHotKey Me.hWnd, &HBFFA&, MOD_CONTROL + MOD_SHIFT, vbKeyL
Me.Show
'等待处理消息
HotKey_Flg = False
Do While Not HotKey_Flg
'等待消息
WaitMessage
'检查是否热键被按下
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
'打开计算器程序
Shell "calc.exe ", vbNormalFocus
End If
'转让控制权,允许操作系统处理其他事件
DoEvents
Loop
End Sub

Private Sub Form_Unload(Cancel As Integer)
HotKey_Flg = True
'撤销热键的注册
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub
问题是我注册了4个快捷键,见上面的代码,无论使用了那个快捷键,只能赋予一个过程或事件(指启动计算器),却不能将这4个快捷键分别赋予不同的过程,请列出使用这4种不同的快捷键分别指定不同的过程的代码。回答正确者在追加50分。急!!!
展开
 我来答
百度网友b922963
2010-06-09 · TA获得超过354个赞
知道小有建树答主
回答量:161
采纳率:0%
帮助的人:0
展开全部
二楼有个错误,
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Dim key As Integer
key = (Message.lParam - 6) / 65536
Select Case key
Case Asc("I")
Debug.Print "按下Ctrl+Shift+I"
Case Asc("K")
Debug.Print "按下Ctrl+Shift+K"
Case Asc("H")
Debug.Print "按下Ctrl+Shift+H"
Case Asc("L")
'打开计算器程序
Shell "calc.exe ", vbNormalFocus
End Select
End If
另外也可以这样
Private Sub Form_Load()
Dim Message As Msg
'注册 Ctrl+Shift+I和Ctrl+Shift+K 为热键
RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL + MOD_SHIFT, vbKeyI
RegisterHotKey Me.hWnd, &HBFFE&, MOD_CONTROL + MOD_SHIFT, vbKeyK
RegisterHotKey Me.hWnd, &HBFFD&, MOD_CONTROL + MOD_SHIFT, vbKeyH
RegisterHotKey Me.hWnd, &HBFFC&, MOD_CONTROL + MOD_SHIFT, vbKeyL
Me.Show
'等待处理消息
HotKey_Flg = False
Do While Not HotKey_Flg
'等待消息
WaitMessage
'检查是否热键被按下
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
'打开计算器程序
Select Case Message.wParam
Case &HBFFF&
Shell "calc.exe ", vbNormalFocus
Case &HBFFE&
'添加事件
Case &HBFFD&
'添加事件
Case &HBFFC&
'添加事件
End Select
End If
'转让控制权,允许操作系统处理其他事件
DoEvents
Loop
End Sub
tianqing20wen

2010-06-09 · TA获得超过2.4万个赞
知道大有可为答主
回答量:8226
采纳率:88%
帮助的人:5100万
展开全部
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Debug.Print (Message.lParam - 6) / 65536
Case Asc("I")
Debug.Print "按下Ctrl+Shift+I"
Case Asc("K")
Debug.Print "按下Ctrl+Shift+K"
Case Asc("H")
Debug.Print "按下Ctrl+Shift+H"
Case Asc("L")
'打开计算器程序
Shell "calc.exe ", vbNormalFocus
End Select
End If

注意注册的快捷键是否被系统中其他程序所占用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
stzcw
2010-06-09 · TA获得超过236个赞
知道小有建树答主
回答量:259
采纳率:0%
帮助的人:0
展开全部
正确如下:
Option Explicit
' 声明API函数
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 PeekMessage Lib "user32 " Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32 " () As Long
' 声明结构
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
' 声明常数
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312

Private HotKey_Flg As Boolean

Private Sub Form_Load()
Dim Message As Msg
'注册 Ctrl+Shift+I和Ctrl+Shift+K 为热键
RegisterHotKey Me.hWnd, &HBFFF&, MOD_CONTROL + MOD_SHIFT, vbKeyI
RegisterHotKey Me.hWnd, &HBFFA&, MOD_CONTROL + MOD_SHIFT, vbKeyK
RegisterHotKey Me.hWnd, &HBFFB&, MOD_CONTROL + MOD_SHIFT, vbKeyH
RegisterHotKey Me.hWnd, &HBFFC&, MOD_CONTROL + MOD_SHIFT, vbKeyL
Me.Show
'等待处理消息
HotKey_Flg = False
Do While Not HotKey_Flg
'等待消息
WaitMessage
'检查是否热键被按下
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
If Message.wParam = &HBFFF& Then
MsgBox "1"
ElseIf Message.wParam = &HBFFA& Then
MsgBox "2"
ElseIf Message.wParam = &HBFFB& Then
MsgBox "3"
ElseIf Message.wParam = &HBFFC& Then
MsgBox "4"
End If
'打开计算器程序
'Shell "calc.exe ", vbNormalFocus

End If
'转让控制权,允许操作系统处理其他事件
DoEvents
Loop
End Sub

Private Sub Form_Unload(Cancel As Integer)
HotKey_Flg = True
'撤销热键的注册
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
Call UnregisterHotKey(Me.hWnd, &HBFFA&)
Call UnregisterHotKey(Me.hWnd, &HBFFB&)
Call UnregisterHotKey(Me.hWnd, &HBFFC&)
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
☆·傻丫丫
2010-06-09
知道答主
回答量:15
采纳率:0%
帮助的人:0
展开全部
启动计算
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式