I have a “wrapper” program that runs with FTR Reporter and I need to capture keystrokes when the user has a log note open but is not recording audio.
In general everything works fine - making a new log note starts capturing keystrokes, closing the log note stops receiving keystrokes, creating a new log note once again starts capturing keystrokes, etc. The problem is related to recording – when I have a log note open and not recording it captures keystrokes and when recording begins it stops capturing keystrokes (as is intended to this point). Once I stop recording however I no longer receive the API callback on a keystroke until I create a new log note (at which point it once again works as intended).
The code is as follows and using the message boxes to “debug” both HookKeyboard and UnhookKeyboard are being called appropriately and the value of KeyboardHandle is never zero. The problem is that once recording has started / stopped KeyboardCallback is never again called until a new log note is created... I would appreciate any input you may have. All I could come up with is that maybe one of the recording related FTR components stops passing keystrokes through.
Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
MsgBox("callback")
'If (Code = HC_ACTION) Then
' PicNotRecording.Visible = False
' TmrNotRecordingFlash.Start()
'End If
Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)
End Function
Public Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
<MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate
Public Sub HookKeyboard()
MsgBox("hook")
Try
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
MsgBox("KeyboardHandle=" & KeyboardHandle.ToString & ", callback=" & callback.ToString)
Catch objException As Exception
WriteEvent("Error connecting to keyboard" & vbCrLf & vbCrLf & objException.Message & vbCrLf & vbCrLf & objException.StackTrace, EventLogEntryType.Error)
End Try
End Sub
Private Function Hooked() As Boolean
Hooked = (KeyboardHandle <> 0)
End Function
Public Sub UnhookKeyboard()
MsgBox("unhook")
Try
If (Hooked()) Then Call UnhookWindowsHookEx(KeyboardHandle)
Catch objException As Exception
WriteEvent("Error disconnecting from keyboard" & vbCrLf & vbCrLf & objException.Message & vbCrLf & vbCrLf & objException.StackTrace, EventLogEntryType.Error)
End Try
End Sub