B4J Library jNativeHookB4J for intercepting system input events

Cableguy

Expert
Licensed User
Longtime User
Actually no, just wanted to report that particular behaviour, that the hook does not work when used inside a class. Later I can post a small example if you have the time to look at it.
In my case I just changed my code a bit and set the hook in Main, so I don't stay stuck on this.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Typically one posts a stack trace and the code that generated that stack trace when reporting errors.
 

Cableguy

Expert
Licensed User
Longtime User
Since the thread was 3 years old, I wasn't expecting much attention...
As I said, I will post all relevant details along with a sample project that shows this behaviour later today, when I get back from my day job
 

Douglas Farias

Expert
Licensed User
Longtime User
Hi @Roycefer
i m testing your lib, and it is working ok.

i found only one problem and i dont know how to fix it.
when i run this line
B4X:
NH.startNativeKeyListener
i get the logs of the pressed keys.

but i cant use special characters like â ã etc.....

on this sub i use return false.
B4X:
Sub NH_NativeKeyPressed(nke As NativeKeyEvent) As Boolean
    Log(nke.KeyText)
    Return False
End Sub

true or false = cant use special characters.
how can i set the listener to listen special characters?

the problem is not the log, the real problem is when i start the listener i cant use special characters on any program, out of b4j code.

For example here: a a a << i tried to use special characters, but the b4j is runing and i cant use, i will stop the debug. á â ã now it works . the listener are blocking like a return true.

thx
 
Last edited:

ThRuST

Well-Known Member
Licensed User
Longtime User
@Douglas Farias I assume also @ and [] chars doesn't work either, correct? And I also suspect that the special chars issue is only related to PC, since I had the same problem on PC but not on Mac, probably because of when running vmWare it uses another keyboard mapping so you might want to check that out and confirm it with a reply. I'm not able to provide any other answer than this, but you can try to detect which key is pressed and then call the ASCII value for it. Not the nicest of solutions, but that might work on PC that is.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Does your keyboard have physical keys for those special characters? Or are you pressing a special key combo to get those special characters? The NativeHook doesn't listen for the user's "intent". It listens for only what keys were pressed.

Do you have event consumption enabled? If so, try turning that off.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I have noticed that ALT is set out of play. Would be good if someone else can confirm this to trace the cause of this. It's not a big deal but annoying in a way. I will do some tests myself to try to locate the problem when I have the time for it.
 

Douglas Farias

Expert
Licensed User
Longtime User

Hi

yes, on brazil special characters are on all keyboards.


the event consumption is off (and return false too).
when i press the special characters i can see the log of the keyname and keycode, but is like it have a return true internal only to special keys, all another keys works fine.


'HERE IS THEN

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private NH As NativeHook
    Private salvaTextos As String
End Sub

Sub AppStart (Args() As String)
    NH.Initialize("NH", Me)
    NH.startNativeKeyListener
    StartMessageLoop
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub NH_NativeKeyPressed(nke As NativeKeyEvent) As Boolean
    salvaTextos = salvaTextos & nke.KeyText
    Log(nke.KeyText)
    Return False
End Sub


Sub NH_Unregistered
    StopMessageLoop
End Sub
Here is what i m using.

i m runing this in debug now, lets try use special key.
a a a a
dont works here on the forum...
i will turn stop the debug.
ã ~ â ^ works.
something internal is blocking this special characters (like a return true on special characters).

on the logs i can see the name of the special characters

Waiting for debugger to connect...
Program started.
A
B
C
D
E
F
Aspas
Crase
Ponto Final
Vírgula
Barra
Indefinido

i will make anothers tests to find a solution.
if you have another sugestion pls send me.

thx
 

Douglas Farias

Expert
Licensed User
Longtime User
can you the not just use
B4X:
if nke.KeyText = "Aspas" then return true
?
Hi @DonManfred
i can see the log "Aspas" but it dont works on any place if the example above is runing.


for example here, on the forum text editor.
the example is runing on debug.
if i press Aspas here is the result: a a a a
if i use
B4X:
    If nke.KeyText = "Aspas" Then
        Log("here")
        Return True
    End If
i can see the log 'here' but i still cant use Aspas on any other place.
if i set it to false i still cant use Aspas.

now if o stop the debug. i can use normal. here : ã ~ â ã ~
the problem is on

B4X:
NH.startNativeKeyListener







Edit: hmmm i found this.
https://github.com/kwhat/jnativehook/issues/213

maybe the error is on JNativeHook jar, and not on the b4j lib. i will try another version...
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
I'm pretty sure I mentioned this in an earlier post but I'll repeat it here. You should not used this library in debug mode. B4X debug mode runs everything in the main thread and that's contrary to this library's threading model. Try it in release mode.

The event subs are supposed to run in their own special thread.
 

Douglas Farias

Expert
Licensed User
Longtime User
Confirmed, the error is on the the version 2.1.0 of JNativeHook on github, and not on your b4j lib.
i m changed to to old version (JNativeHook v2.0.3) jar file, and now all is working fine (on debug or release).

thx all.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…