用API函數(shù),以下代碼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
創(chuàng)新互聯(lián)建站是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷策劃、成都微信小程序、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動(dòng)互聯(lián)開(kāi)發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立十多年以來(lái),已經(jīng)為1000多家水電改造各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)?,F(xiàn)在,服務(wù)的1000多家客戶與我們一路同行,見(jiàn)證我們的成長(zhǎng);未來(lái),我們一起分享成功的喜悅。
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) '調(diào)用這個(gè)過(guò)程即可對(duì)目標(biāo)窗體發(fā)送F1鍵
Call PostMessage(hwnd, WM_KEYDOWN, VK_F1, 0)
Call PostMessage(hwnd, WM_KEYUP, VK_F1, 0)
End Sub這里是發(fā)送F1的實(shí)例,各個(gè)按鍵對(duì)應(yīng)的虛擬鍵碼,要到網(wǎng)上查
1、將窗口的KeyPreview屬性設(shè)置為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相當(dāng)于單擊按鈕
還是焦點(diǎn)不在程序里面的時(shí)候任意按q實(shí)現(xiàn)這個(gè)功能?
要是前者的話樓上的已經(jīng)正解
要是后者的話需要全局鉤子實(shí)現(xiàn)