VB设置热键
发布网友
发布时间:2022-04-28 11:29
我来回答
共2个回答
热心网友
时间:2023-10-05 11:44
这个也简单,我给你做了个全局热键:代码我都帮你综合了你自己别忘了控件基本不会出错的!1楼下的P话一样的你要窗体上KeyPress事件我还不如直接点按钮呢你说是不?还有你的窗体不是Form1的话就自己在模块里修改下嘎嘎
窗体代码:
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 Sub Command1_Click()
Text4 = Val(Text2) * Val(Text3)
Timer1.Enabled = True
MsgBox "自动喊话开启成功!谢谢您的使用!" '这个一段代码的意思,当按下command1按钮时,将text2和text3的数值想乘,然后把数值赋予给text4,并开启time控件开始喊话。
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
MsgBox "自动喊话已关闭!谢谢您的使用!" '这段代码的意思是关闭time控件,关闭自动喊话器。
End Sub
Private Sub Form_Load()
HooK
Command1.Caption = "开始自动喊话"
Command2.Caption = "结束自动喊话"
Timer1.Enabled = False
Text1.Text = "请在这里输入你要喊话的内容!" '这些代码意思是当窗体载入的时候,将command1,command2,text1的内容换成后面的。并关闭time控件!
End Sub
Private Sub Label3_Click()
Shell "explorer.exe http://user.qzone.qq.com/282718156"
End Sub
Private Sub Text3_Change()
For i = 0 To 1
If Val(Text3.Text) < 1 Or Val(Text3.Text) > 60 Then
MsgBox "请输1秒到60秒之间的数值,建议大于10秒!" '这个代码的意思检测text3里面输入的数字!当小于1秒或者大于60秒时清空text3里面的数值!
Text3.Text = ""
Text3.SetFocus
Exit Sub
End If
Next i
End Sub
Private Sub Timer1_Timer()
Timer1.Interval = Text4.Text '这个代码的意思是 time控件的时间属性由text4提供。并在规定的时间到达时,在鼠标的准星处输入text1中的内容,并按下enter键。
SendKeys Text1.Text
SendKeys "{enter}"
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHooK
End Sub
模块代码:
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13
Private lngHook As Long
Public Function HotKey(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim p As PKBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
Call CopyMemory(p, ByVal lParam, Len(p))
If p.vkCode = vbKeyF2 Then '开始
Form1.Text4 = Val(Text2) * Val(Text3)
Form1.Timer1.Enabled = True
MsgBox "自动喊话开启成功!谢谢您的使用!" '这个一段代码的意思,当按下command1按钮时,将text2和text3的数值想乘,然后把数值赋予给text4,并开启time控件开始喊话。
ElseIf p.vkCode = vbKeyF3 Then '暂停
Form1.Timer1.Enabled = False
MsgBox "自动喊话已关闭!谢谢您的使用!" '这段代码的意思是关闭time控件,关闭自动喊话器。
End If
Case Else
End Select
End If
Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)
End Function
Public Sub HooK()
lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf HotKey, App.hInstance, 0)
End Sub
Public Sub UnHooK()
Call UnhookWindowsHookEx(lngHook)
End Sub
这样你只要在任何地方按下F2都能激活窗体代码开始,按下F3都能暂停!
有什么不懂的可以留个邮箱哈
热心网友
时间:2023-10-05 11:45
Private Sub Form_KeyPress(KeyAscii As Integer)
On Error Resume Next
Dim Key
Key = Chr(KeyAscii)
If Key = "x" Then
Call Command1_Click()
End If
End Sub '按x就会启动Command1_Click()
--是用这个吗
热心网友
时间:2023-10-05 11:44
这个也简单,我给你做了个全局热键:代码我都帮你综合了你自己别忘了控件基本不会出错的!1楼下的P话一样的你要窗体上KeyPress事件我还不如直接点按钮呢你说是不?还有你的窗体不是Form1的话就自己在模块里修改下嘎嘎
窗体代码:
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 Sub Command1_Click()
Text4 = Val(Text2) * Val(Text3)
Timer1.Enabled = True
MsgBox "自动喊话开启成功!谢谢您的使用!" '这个一段代码的意思,当按下command1按钮时,将text2和text3的数值想乘,然后把数值赋予给text4,并开启time控件开始喊话。
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
MsgBox "自动喊话已关闭!谢谢您的使用!" '这段代码的意思是关闭time控件,关闭自动喊话器。
End Sub
Private Sub Form_Load()
HooK
Command1.Caption = "开始自动喊话"
Command2.Caption = "结束自动喊话"
Timer1.Enabled = False
Text1.Text = "请在这里输入你要喊话的内容!" '这些代码意思是当窗体载入的时候,将command1,command2,text1的内容换成后面的。并关闭time控件!
End Sub
Private Sub Label3_Click()
Shell "explorer.exe http://user.qzone.qq.com/282718156"
End Sub
Private Sub Text3_Change()
For i = 0 To 1
If Val(Text3.Text) < 1 Or Val(Text3.Text) > 60 Then
MsgBox "请输1秒到60秒之间的数值,建议大于10秒!" '这个代码的意思检测text3里面输入的数字!当小于1秒或者大于60秒时清空text3里面的数值!
Text3.Text = ""
Text3.SetFocus
Exit Sub
End If
Next i
End Sub
Private Sub Timer1_Timer()
Timer1.Interval = Text4.Text '这个代码的意思是 time控件的时间属性由text4提供。并在规定的时间到达时,在鼠标的准星处输入text1中的内容,并按下enter键。
SendKeys Text1.Text
SendKeys "{enter}"
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHooK
End Sub
模块代码:
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13
Private lngHook As Long
Public Function HotKey(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim p As PKBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
Call CopyMemory(p, ByVal lParam, Len(p))
If p.vkCode = vbKeyF2 Then '开始
Form1.Text4 = Val(Text2) * Val(Text3)
Form1.Timer1.Enabled = True
MsgBox "自动喊话开启成功!谢谢您的使用!" '这个一段代码的意思,当按下command1按钮时,将text2和text3的数值想乘,然后把数值赋予给text4,并开启time控件开始喊话。
ElseIf p.vkCode = vbKeyF3 Then '暂停
Form1.Timer1.Enabled = False
MsgBox "自动喊话已关闭!谢谢您的使用!" '这段代码的意思是关闭time控件,关闭自动喊话器。
End If
Case Else
End Select
End If
Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)
End Function
Public Sub HooK()
lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf HotKey, App.hInstance, 0)
End Sub
Public Sub UnHooK()
Call UnhookWindowsHookEx(lngHook)
End Sub
这样你只要在任何地方按下F2都能激活窗体代码开始,按下F3都能暂停!
有什么不懂的可以留个邮箱哈
热心网友
时间:2023-10-05 11:45
Private Sub Form_KeyPress(KeyAscii As Integer)
On Error Resume Next
Dim Key
Key = Chr(KeyAscii)
If Key = "x" Then
Call Command1_Click()
End If
End Sub '按x就会启动Command1_Click()
--是用这个吗
热心网友
时间:2023-10-05 11:44
这个也简单,我给你做了个全局热键:代码我都帮你综合了你自己别忘了控件基本不会出错的!1楼下的P话一样的你要窗体上KeyPress事件我还不如直接点按钮呢你说是不?还有你的窗体不是Form1的话就自己在模块里修改下嘎嘎
窗体代码:
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 Sub Command1_Click()
Text4 = Val(Text2) * Val(Text3)
Timer1.Enabled = True
MsgBox "自动喊话开启成功!谢谢您的使用!" '这个一段代码的意思,当按下command1按钮时,将text2和text3的数值想乘,然后把数值赋予给text4,并开启time控件开始喊话。
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
MsgBox "自动喊话已关闭!谢谢您的使用!" '这段代码的意思是关闭time控件,关闭自动喊话器。
End Sub
Private Sub Form_Load()
HooK
Command1.Caption = "开始自动喊话"
Command2.Caption = "结束自动喊话"
Timer1.Enabled = False
Text1.Text = "请在这里输入你要喊话的内容!" '这些代码意思是当窗体载入的时候,将command1,command2,text1的内容换成后面的。并关闭time控件!
End Sub
Private Sub Label3_Click()
Shell "explorer.exe http://user.qzone.qq.com/282718156"
End Sub
Private Sub Text3_Change()
For i = 0 To 1
If Val(Text3.Text) < 1 Or Val(Text3.Text) > 60 Then
MsgBox "请输1秒到60秒之间的数值,建议大于10秒!" '这个代码的意思检测text3里面输入的数字!当小于1秒或者大于60秒时清空text3里面的数值!
Text3.Text = ""
Text3.SetFocus
Exit Sub
End If
Next i
End Sub
Private Sub Timer1_Timer()
Timer1.Interval = Text4.Text '这个代码的意思是 time控件的时间属性由text4提供。并在规定的时间到达时,在鼠标的准星处输入text1中的内容,并按下enter键。
SendKeys Text1.Text
SendKeys "{enter}"
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHooK
End Sub
模块代码:
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Type PKBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYUP = &H105
Private Const HC_ACTION = 0
Private Const WH_KEYBOARD_LL = 13
Private lngHook As Long
Public Function HotKey(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim p As PKBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
Call CopyMemory(p, ByVal lParam, Len(p))
If p.vkCode = vbKeyF2 Then '开始
Form1.Text4 = Val(Text2) * Val(Text3)
Form1.Timer1.Enabled = True
MsgBox "自动喊话开启成功!谢谢您的使用!" '这个一段代码的意思,当按下command1按钮时,将text2和text3的数值想乘,然后把数值赋予给text4,并开启time控件开始喊话。
ElseIf p.vkCode = vbKeyF3 Then '暂停
Form1.Timer1.Enabled = False
MsgBox "自动喊话已关闭!谢谢您的使用!" '这段代码的意思是关闭time控件,关闭自动喊话器。
End If
Case Else
End Select
End If
Call CallNextHookEx(WH_KEYBOARD_LL, nCode, wParam, lParam)
End Function
Public Sub HooK()
lngHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf HotKey, App.hInstance, 0)
End Sub
Public Sub UnHooK()
Call UnhookWindowsHookEx(lngHook)
End Sub
这样你只要在任何地方按下F2都能激活窗体代码开始,按下F3都能暂停!
有什么不懂的可以留个邮箱哈
热心网友
时间:2023-10-05 11:45
Private Sub Form_KeyPress(KeyAscii As Integer)
On Error Resume Next
Dim Key
Key = Chr(KeyAscii)
If Key = "x" Then
Call Command1_Click()
End If
End Sub '按x就会启动Command1_Click()
--是用这个吗