I noticed that some people were using the jNativeHookB4J library to allow their programs to listen for key combos (ex: control_shift_f). I thought I'd make your lives a little easier and more flexible with the attached class. The KeyComboListener allows you to listen for key combos using the jNativeHookB4J library and is better than the old method (creating Global Booleans to track when certain keys are in the down position) because you can create and watch for new key combos at run-time. This means that your users can create their own key combos without you having to hard-code each one in. Furthermore, this class raises its event sub in the Main Thread, which will be more intuitive.
Example:
While you should only create one NativeHook Object per program, you can create as many KeyComboListeners as you like.
As usual, if you make any interesting modifications to the KeyComboListener code, feel free to share them in this thread.
Example:
B4X:
Sub Process_Globals
Dim nh As NativeHook
Dim kcl As KeyComboListener
End Sub
Sub AppStart (Args() As String)
kcl.Initialize(Me,"kcl", Array("control", "shift", "f")).StartListening
'You can use "control" to trigger on any ctrl press or "left control" to trigger on the left one, only.
'Same for "shift", etc...
kcl.RaiseEvent 'We can programmatically raise the event if we want
nh.Initialize("nh", Me).startNativeKeyListener
'In real code, don't forget to unregister your NativeHook when you're done with it!!!
StartMessageLoop
End Sub
Sub nh_NativeKeyPressed(nke As NativeKeyEvent) As Boolean
kcl.KeyPressed(nke) ' <----You MUST call this inside nh_NativeKeyPressed!!!!!!!
Return False
End Sub
Sub nh_NativeKeyReleased(nke As NativeKeyEvent) As Boolean
kcl.KeyReleased(nke) ' <----You MUST call this inside nh_NativeKeyReleased!!!!!!!
Return False
End Sub
'This will be executed when we call kcl.RaiseEvent or when the user presses the control_shift_f key combo
Sub kcl_KeyComboPressed
Log(kcl.GetKeyComboStringRepresentation & " pressed.")
Log(" ")
End Sub
While you should only create one NativeHook Object per program, you can create as many KeyComboListeners as you like.
As usual, if you make any interesting modifications to the KeyComboListener code, feel free to share them in this thread.