Use VB to register the hotkey in the application

zhaozj2021-02-16  65

'Form Option Explicit

Private Sub Form_Load () Dim ret As Long 'recorded original program address window preWinProc = GetWindowLong (Me.hWnd, GWL_WNDPROC)' with a custom application program window instead of the original ret = SetWindowLong (Me.hWnd, GWL_WNDPROC, AddressOf wndproc) idHotKey = 1 'in the range & h0000 through & hBFFF Modifiers = MOD_ALT' secondary key is Alt uVirtKey1 = vbKeyQ 'registered for the hot key Alt Q' Register hotkey ret = RegisterHotKey (Me.hWnd, idHotKey, Modifiers, uVirtKey1) If ret = 0 THEN MSGBOX "Registering hot keys fail, please use other hot keys!", Vbcritical, "Error" End IFEND SUB

Private Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer) Dim ret As Long 'Cancel Message taken, so that sent the original window procedure ret = SetWindowLong (Me.hWnd, GWL_WNDPROC, preWinProc) Call UnregisterHotKey (Me.hWnd, uVirtKey1 ) End Sub

'Module

'The following procedure is placed in the module Option Explicit

Declare Function SetWindowLong Lib "user32" Alias ​​"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongDeclare Function GetWindowLong Lib "user32" Alias ​​"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As LongDeclare Function CallWindowProc Lib "user32" Alias ​​"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongDeclare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, Byval ID as long, byval fsmodifiers as longdeclare function unregisterhotkey lib "user32" (Byval HWnd As long) (BYVAL ID As long)

Public Const WM_HOTKEY = & H312Public Const MOD_ALT = & H1Public Const MOD_CONTROL = & H2Public Const MOD_SHIFT = & H4Public Const GWL_WNDPROC = (-4) Public preWinProc As LongPublic Modifiers As Long, uVirtKey1 As Long, idHotKey As Long

Private Type Talong Ll As Longend Type

Private Type T2int LWORD AS INTEGER HWORD AS INTEGEREND TYPE PUBLIC FUND WND, BYVAL MSG As Long, Byval WParam As Long, Byval LParam As Longdim LP As Talong, i2 as T2Int

IF msg = WM_HOTKEY THEN IF WPARAM = IDHOTKEY THEN LP.LL = LPARAM LSET I2 = LP if (i2.lword = modifiers) and i2.hword = uvirtkey1 Then Form1.visible = not form1.visible end if end if endiff1.visible end if End if Not hot key information calls the original program WndProc = CallWindowProc (PrewinProc, HWnd, MSG, WPARAM, LPARAM) End Function

转载请注明原文地址:https://www.9cbs.com/read-16974.html

New Post(0)