用API函数,以下代码Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
创新互联建站是一家专注网站建设、网络营销策划、成都微信小程序、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立十多年以来,已经为1000多家水电改造各业的企业公司提供互联网服务。现在,服务的1000多家客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As LongPrivate Const WS_EX_LAYERED = H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = H2
Private Const LWA_COLORKEY = H1Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMosT = -1
Private Const SWP_NOMOVE = H2
Private Const SWP_NosIZE = H1
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_KEYDOWN = H100
Const WM_KEYUP = H101
Const WM_CHAR = H102
Const VK_F1 = H70
Private Sub SendF1(hwnd) '调用这个过程即可对目标窗体发送F1键
Call PostMessage(hwnd, WM_KEYDOWN, VK_F1, 0)
Call PostMessage(hwnd, WM_KEYUP, VK_F1, 0)
End Sub这里是发送F1的实例,各个按键对应的虚拟键码,要到网上查
1、将窗口的KeyPreview属性设置为True
Me.KeyPreview = True
2、处理窗口的KeyDown事件
Sub MainFormKeyDown(sender As Object, e As KeyEventArgs)
Select Case e.KeyCode
Case Keys.Escape
Me.button1.PerformClick
Case Keys.Enter
Me.button2.PerformClick
End Select
End Sub
你是要在程序里面按q相当于单击按钮
还是焦点不在程序里面的时候任意按q实现这个功能?
要是前者的话楼上的已经正解
要是后者的话需要全局钩子实现