I’m capturing the keyboard keys that are pressed by the CreateKeyboardHook event, where it returns all the keys that were pressed (making it a keylogger, without storing or anything like that, just printing to the console)
Sub Main()
Try
Dim Kbd As New KeyboardStroke
Dim hookTask As Task = Task.Run(Sub()
Kbd.CreateKeyboardHook(Sub(keyPressed)
Console.Write(keyPressed)
End Sub)
Application.Run()
End Sub)
hookTask.Wait()
Catch ex As Exception
End Try
End Sub
I “ran into” a problem (I don’t know if it can be said to be a problem, as it is a function that listens to the keyboard keys) which is the issue of accentuation. I did several searches to try to combine accent + letter and didn’t find anything related (any function specifically for this)
Would there be any way to deal with this? I thought of something related to recording the last key pressed and if that key is an accent, it waits for the next letter or key pressed, so if it is a vowel, it would combine the accent + the vowel (as vowels are the only letters that receive an accent , different from consonants..)
However, I was unable to apply or find any function that performs this “correction”.
Of course this is something specific, like pt-BR keyboard layouts (for example)
The CreateKeyboardHook
function follows:
Public Sub CreateKeyboardHook(ByVal keyPressedCallback As Action(Of KeyPressed))
Me.keyPressedCallback = keyPressedCallback
Me.HookKeyboardDelegate = AddressOf HookKeyboardCallbackImplementation
Me.globalKeyboardHookId = User32.SetWindowsHookEx(WH_KEYBOARD_LL, Me.HookKeyboardDelegate, Me.currentModuleId, 0)
End Sub
For a better understanding (avoid huge codes, but if necessary I will share them with you..)
-
the
keyPressedCallback
parameter is executed whenever the key is pressed (it is a way of defining what should happen when a keyboard event occurs) -
the
SetWindowsHookEx
method is used to install a keyboard hook procedure, it receives some parameters such as the hook type (in this case WH_KEYBOARD_LL) a pointer to the function that will be called whenever a keyboard event occurs (the HookKeyboardDelegate), the handle of the current module, and a handle to the thread associated with the keyboard hook. -
the
HookKeyboardCallbackImplementation
method is the function that will be called whenever a keyboard event occurs. It receives event information, such as the key code (nCode), the type of message (wParam) and a pointer to a structure that contains information about the key (lParam) here is where the actual processing of keyboard events happens
Below is a capture of the event: