设置窗体的keypreview属性为true,例子代码如下Option ExplicitPrivate Sub Command1_Click() MsgBox "hello" End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyF3 Command1_Click End Select End Sub
添加一个timer 和 command1Option Explicit Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer '判断函数调用时指定虚拟键的状态 Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long '查找与指定文件关联在一起的程序的文件名 Dim HyperJump Private Sub Command1_Click() HyperJump = ShellExecute(0&, vbNullString, "http://sun.ccthtf.com.cn/", vbNullString, vbNullString, vbNormalFocus) End Sub Private Sub Form_Load() With Timer1 .Interval = 1 End With End Sub Private Sub Timer1_Timer() If MyHotKey(vbKeyF3) Then Command1_Click End Sub Private Function MyHotKey(vKeyCode) As Boolean MyHotKey = (GetAsyncKeyState(vKeyCode) < 0) End Function
SendMessage(me.hwnd,WM_SETHOTKEY,VK_F3 )
MsgBox "hello"
End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF3
Command1_Click
End Select
End Sub
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
'判断函数调用时指定虚拟键的状态
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'查找与指定文件关联在一起的程序的文件名
Dim HyperJump
Private Sub Command1_Click()
HyperJump = ShellExecute(0&, vbNullString, "http://sun.ccthtf.com.cn/", vbNullString, vbNullString, vbNormalFocus)
End Sub
Private Sub Form_Load()
With Timer1
.Interval = 1
End With
End Sub
Private Sub Timer1_Timer()
If MyHotKey(vbKeyF3) Then Command1_Click
End Sub
Private Function MyHotKey(vKeyCode) As Boolean
MyHotKey = (GetAsyncKeyState(vKeyCode) < 0)
End Function