Hi All,
I found the below code on a website somewhere and have bunged it into an Access module.
I then register the key on a form like soCode:Option Compare Database Public Const GWL_WNDPROC As Long = (-4) Public Const WM_HOTKEY As Long = &H312 Public Const MOD_ALT = &H1 Public Const MOD_CONTROL = &H2 Public Const MOD_SHIFT = &H4 Public Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" _ (ByVal lpString As String) As Long Public Declare Function GlobalDeleteAtom Lib "kernel32" _ (ByVal nAtom As Long) As Long Public Declare Function RegisterHotKey Lib "user32" _ (ByVal hWnd As Long, _ ByVal id As Long, _ ByVal fsModifiers As Long, _ ByVal vk As Long) As Long Public Declare Function UnregisterHotKey Lib "user32" _ (ByVal hWnd As Long, _ ByVal id As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ (ByVal hWnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long Public Declare 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 Long Public mlngLastWndProc As Long Public mlngAtom As Long ' Functions and Subroutines to store in main module ' Function which determines what happens when HotKey pressed Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long If uMsg = WM_HOTKEY Then ' MsgBox "firex" End If WindowProc = CallWindowProc(mlngLastWndProc, hWnd, uMsg, wParam, lParam) End Function ' Subroutine which creates the Hot Key Public Sub CreateHotKey(ByVal intKeyCode As Integer, ByVal hWnd As Long) mlngAtom = GlobalAddAtom(CStr(Now)) mlngLastWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc) RegisterHotKey hWnd, mlngAtom, 0, intKeyCode End Sub ' Subroutine which destroys the hot key Public Sub DestroyHotKey(ByVal hWnd As Long) UnregisterHotKey hWnd, GlobalDeleteAtom(mlngAtom) Call SetWindowLong(hWnd, GWL_WNDPROC, mlngLastWndProc) End Sub
Cool I thought I press F2 and up comes my msgbox, I then looked at my processor on my PC and it was going nuts, and basically this ends up causing all of Access to crash as it constantly suck massive amounts of processsor time.Code:CreateHotKey vbKeyF2, Me.hWnd
I don't understand the code I'm using which is allways a nightmare, so have no idea how to fix this. Any ideas anyone?
I just want a hotkey in access that works from anywhere.


Reply With Quote