如何在VB里设置个全局快捷键?

要用哪个函数请写出被调用函数的意思,谢谢!... 要用哪个函数 请写出被调用函数的意思,谢谢! 展开
 我来答
Finding___
2008-09-15 · TA获得超过2万个赞
知道大有可为答主
回答量:7100
采纳率:0%
帮助的人:4370万
展开全部
'-------------------------类模块中(class1)-----------------
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 Type KeyMsg
ID As Long ' 保存注册热键时的ID
Key As String '保存注册热键时的关键字
End Type

Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312

Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal ID As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
'id 值范围 :0X0000-0XBFFF
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
'************************************************************

Enum ShiftKeys
AltKey = &H1
CtrlKey = &H2
ShiftKey = &H4
End Enum

'局部变量
Private bCancel As Boolean
Private clsHwnd As Long
Private KeyGroup As Integer
Private KeyID As Long
Private Keys() As KeyMsg

'声明事件
Public Event HotKeysDown(Key As String)

'注册热键,可以注册多组热键
Sub RegHotKeys(ByVal hwnd As Long, ByVal ShiftKey As ShiftKeys, ByVal ComKey As KeyCodeConstants, ByVal Key As String)
On Error Resume Next
clsHwnd = hwnd
KeyID = KeyID + 1
KeyGroup = KeyGroup + 1
ReDim Preserve Keys(KeyGroup)
RegisterHotKey hwnd, KeyID, ShiftKey, ComKey '注册热键
Keys(KeyGroup).ID = KeyID
Keys(KeyGroup).Key = Trim(Key)
End Sub

'取消热键注册
Sub UnRegHotKeys(ByVal Key As String)
On Error Resume Next
If KeyGroup = 0 Then Exit Sub
Dim i As Integer
For i = 0 To KeyGroup
If Trim(Key) = Trim(Keys(i).Key) Then
UnregisterHotKey clsHwnd, Keys(i).ID
End If
Next
End Sub

'取消全部热键注册
Sub UnRegAllHotKeys()
On Error Resume Next
If KeyGroup = 0 Then Exit Sub
Dim i As Integer
For i = 0 To KeyGroup
UnregisterHotKey clsHwnd, Keys(i).ID
Next
End Sub

'等候按键消息
Sub WaitMsg()
On Error Resume Next
bCancel = False
Dim Message As Msg, i As Integer
Do While Not bCancel
WaitMessage '等候按键消息
'判断消息
If PeekMessage(Message, clsHwnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
For i = 0 To KeyGroup
If Keys(i).ID = Message.wParam Then '判断按下哪组热键
RaiseEvent HotKeysDown(Keys(i).Key) '引发事件
End If
Next
End If
DoEvents
Loop
End Sub

'取消等候消息
Sub UnWaitMsg()
bCancel = True
End Sub

Private Sub Class_Initialize()
KeyID = &H1000& '初始ID
KeyGroup = -1
ReDim Keys(0)
End Sub

Private Sub Class_Terminate()
On Error Resume Next
bCancel = True
UnRegAllHotKeys
End Sub

'-------------------窗体中-----------------------
Private Sub Form_Load()
'注册热键
Set hk = New Class1
'注册全局热键,可注册多个热键
'______________________第一个键--第二个键--名称
hk.RegHotKeys Me.hwnd, CtrlKey, vbKeyQ, "Q"
hk.RegHotKeys Me.hwnd, CtrlKey, vbKeyW, "W"
'如果注册的热键已经被别的程序占用则无效
Me.Show '这个不能省略,否则窗体无法显示出来!
hk.WaitMsg
End Sub

Private Sub hk_HotKeysDown(Key As String)
Select Case Key
Case "Q" 'Case 后面跟注册的热键的名称
'按下第一个热键的代码
Case "W"
'按下第二个热键的代码
End Sub

'退出程序时一定要加一个End语句,否则只是隐藏窗口,并没有结束程序
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
smh1982
2008-09-09 · TA获得超过843个赞
知道小有建树答主
回答量:1578
采纳率:0%
帮助的人:885万
展开全部
将窗口的KeyPreview设置为True,然后在窗口的Key_Down里写代码。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
樱凌准
2008-09-09 · TA获得超过243个赞
知道小有建树答主
回答量:259
采纳率:0%
帮助的人:260万
展开全部
用GetAsyncKeyState()函数.
声名如下:
Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer

其中的vKey就是要检测的按键code.

相应键按下时,函数返回值小于零.

MSDN里对此函数的说明如下:

GetAsyncKeyState Function

--------------------------------------------------------------------------------

The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

Syntax

SHORT GetAsyncKeyState( int vKey
);
Parameters

vKey
[in] Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes.
Windows NT/2000/XP: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information.

Return Value

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

Windows NT/2000/XP: The return value is zero for the following cases:

The current desktop is not the active desktop
The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.
Windows 95/98/Me: The return value is the global asynchronous key state for each virtual key. The system does not check which thread has the keyboard focus.

Windows 95/98/Me: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.

Remarks

The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling

GetSystemMetrics(SM_SWAPBUTTON)

which returns TRUE if the mouse buttons have been swapped.

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.

You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the SHIFT, CTRL, or ALT keys without distinguishing between left and right.

Windows NT/2000/XP: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys.

Code Meaning
VK_LSHIFT VK_RSHIFT
VK_LCONTROL VK_RCONTROL
VK_LMENU VK_RMENU

These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.

Function Information

Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式