This is how I'd do it:
Compile this code to a shared dll called F12_Hook.dll :
Code: Select all
#PerformActionMessage = #WM_APP + 1
#UnhookandEndMessage = #WM_APP + 2
Global KeyboardHook, CallingWindow
ProcedureDLL Attachprocess(instance)
CompilerIf Defined(KBDLLHOOKSTRUCT, #PB_Structure) = 0
Structure KBDLLHOOKSTRUCT
vkCode.l
scanCode.l
flags.l
time.l
dwExtraInfo.l
EndStructure
CompilerEndIf
EndProcedure
ProcedureDLL KeyboardProc(ncode,wparam,lparam)
*keycode.KBDLLHOOKSTRUCT=lparam
If wParam = #WM_KEYDOWN
Select *keycode\vkCode
Case #VK_F12
PostMessage_(CallingWindow, #PerformActionMessage, 0, 0)
ProcedureReturn 1
Case #VK_F11
PostMessage_(CallingWindow, #UnhookandEndMessage, 0, 0)
ProcedureReturn 1
EndSelect
EndIf
ProcedureReturn CallNextHookEx_( KeyboardHook, nCode, wParam, lParam )
EndProcedure
ProcedureDLL InitHook(hwnd, hook)
CallingWindow = hwnd
KeyboardHook = hook
EndProcedure
Compile this code to a Windows exe called Testhook.exe, place it in same folder as F12_Hook.dll:
Code: Select all
;=====================================================================================================
; Offtopic: Sets up the timed messagebox for status display
;=====================================================================================================
OpenLibrary (0, "user32.dll")
Prototype MessageBoxTimed(hWnd.l, lpText.s,lpCaption.s, uType.l, wLanguageID.w, dwMilliseconds.l)
Global MessageBoxTimed.MessageBoxTimed = GetFunction(0,"MessageBoxTimeoutA")
;=====================================================================================================
; End of Offtopic section
;=====================================================================================================
#PerformActionMessage = #WM_APP + 1
#UnhookandEndMessage = #WM_APP + 2
OpenWindow(0,0,0,0,0,"Testhook",#PB_Window_BorderLess|#PB_Window_Invisible)
hooklib = OpenLibrary(0, "F12_Hook.dll")
If hooklib
keyproc=GetFunction(0, "KeyboardProc")
KeyboardHook=SetWindowsHookEx_(#WH_KEYBOARD_LL, keyproc, hooklib, 0)
CallFunction(0, "InitHook", WindowID(0), KeyboardHook)
MessageRequester("Notice:","Press F12 to perform action, F11 to unhook and unload this program",#MB_OK|#MB_ICONINFORMATION)
EndIf
Repeat
ev = WaitWindowEvent()
Select ev
Case #WM_CLOSE, #UnhookandEndMessage
MessageBoxTimed(0,"Unhooking and ending, thanks for testing!","Action:",#MB_OK|#MB_SYSTEMMODAL|#MB_ICONINFORMATION, 0, 1000)
Break
Case #PerformActionMessage
MessageBoxTimed(0,"Performing the keyboard hook action","Action:",#MB_OK|#MB_SYSTEMMODAL|#MB_ICONINFORMATION, 0, 1000)
EndSelect
ForEver
UnhookWindowsHookEx_(KeyboardHook)
Run the Testhook.exe program and press F12 from anywhere in the system, then press F11 to end the test session and unload the hook and test program.
Note that my dll is returning 1 when the key is trapped, which prevents the application having the keyboard focus from getting it at all. If you want the keypress not to lose its effect in other apps, return 0 instead and it will send your window the message while still allowing the other app to process the key normally.