B4J Library jNativeHookB4J for intercepting system input events

Roycefer

Well-Known Member
Licensed User
Longtime User
This library works in UI apps. However, you won't be able to directly access UI elements from the NativeHook events since they run in a separate thread. Instead, you'll have to use Thread.RunOnGuiThread() to access UI elements (from the Threading library). I don't think this is your problem, though.

Are you sure you put the JNativeHook 2.0.2 jar in your B4J external libraries folder (not the B4A one)? What computer and OS are you using?
 

Suntzu

Member
Licensed User
Longtime User
Yes, JNativeHook 2.0.2 jar is in the B4J external libraries folder. I'm on a Sony VAIO laptop, Windows Vista Home SP2.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Were you able to run any of the sample code from this thread in a non-UI app? Also note that sometimes your anti-virus may attempt to block this library (for obvious reasons).
 

Suntzu

Member
Licensed User
Longtime User
I tried the example from your original post on a barebone project and got the same error.

Good idea. I will exclude the jar and related files in my antivirus.

Thank you.
 

MbedAndroid

Active Member
Licensed User
Longtime User
trying this tool i encounter a build error line 327 RobotDelay
Any idea?
B4J version: 3.71
Parsing code. (0.01s)
Compiling code. Error
Error compiling program.
Error description: Unknown member: robotdelay
Occurred on line: 327
lore.RobotDelay(10)
Word: robotdelay

Edit: just changed the provided libs, problem solved!
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
The NativeMouseWheelEvent object is passed to the _NativeMouseWheelMoved() event sub as an argument, holding all the pertinent info about the mouse wheel event (amount, direction, etc... all available in the code-completion menu). You shouldn't be trying to create objects of type NativeMouseWheelEvent.
B4X:
Sub nh_NativeMouseWheelMoved(nmwe As NativeMouseWheelEvent) As Boolean
    Log("Scroll Type: " & nmwe.ScrollType)
    Log("Scroll Amount: " & nmwe.ScrollAmount)
    Log("Wheel Rotation: " & nmwe.WheelRotation)
    Return False
End Sub
Obviously, you need to start the appropriate Listener with nh.startNativeMouseWheelListener().

As usual, don't forget to unregister your NativeHook when you're done using it.
 

Hanstel

Member
Licensed User
is there a similar Library for web application? or taking system event from PC/Laptop Webcam aside from keyboard and mouse?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
There's an incomplete webcam library somewhere in the libraries forum. A search should reveal it. The problem is that there aren't very many good, complete Java webcam libraries to wrap. Anyhow, that's a topic entirely separate from this thread.

As for intercepting system input events in web applications, do you mean in the browser via a JS API? I don't know of any standard JavaScript APIs that expose system input events through the browser. And it seems like doing so would be a yuge security violation. It might be possible using ActiveX or some other browser plugins. I'm not entirely sure.
 

Hanstel

Member
Licensed User
yes at least on the browser? how do you wrap an existing Javascript (like keydrown), still newbie and learning b4j
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You should study some of the WebSocket examples and familiarize yourself with some JavaScript and JQuery. Once you get good with those, you'll be able to have your B4J server run arbitrary JavaScript code in the client browser via WebSocket.Eval() and related methods. One doesn't really "wrap" JavaScript libraries in B4J. But if the HTML document includes a <script> reference to a JS library, you could package a bunch of calls to that JS library using WebSocket.Eval() into a B4J class and compile it into a library.
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Hi @Roycefer

I got everything working but am trying to tram CTRL and SHIFT too.

B4X:
Sub NH_NativeKeyPressed(nke As NativeKeyEvent) As Boolean
    Dim Shift As Int = 16
    Dim Ctrl As Int = 17
    If NH.isCharacter(nke.keyCode) And NH.isSpecialKey(Shift) And NH.isSpecialKey(Ctrl) Then
        Log(nke.keyText & " Pressed")
        If nke.keyText=="Z" Then
            Log("Got it!")
            Return True    'Consumes the event if the user pressed the "F" key
        End If
    End If
    Return False

End Sub

What am I missing? If I just grab the key it is fine.

Thanks.
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
It looks like you are trying to identify when the user uses the ctrl_shift_f key combo. Is that correct?

If so, I recommend doing the following. Create Process_Global Boolean variables called ShiftDown and CtrlDown (both initially set to False). Set them to True inside NH_NativeKeyPressed if the NativeKeyEvent indicates the corresponding key was pressed with code that looks something like this:
B4X:
If nke.KeyText.ToLowerCase.Contains("control") Then CtrlDown = True    'this catches both Left Control and Right Control. Do the same with the Shift keys.
Don't forget to do the inverse in NH_NativeKeyReleased. Then, later on inside NH_NativeKeyPressed, you can check if the F key is pressed and if it is, you can check to see if CtrlDown and ShiftDown are both true. If so, then you know ctrl_shift_f was entered by the user.

Remember that each key press with generate its own Event. A 3-key combo like ctrl_shift_f will generate three events so we use Process_Global variables to communicate state between them.
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Running into this error but ONLY with the NH_NativeKeyReleased sub. If I remove it the all is good.


 

Roycefer

Well-Known Member
Licensed User
Longtime User
It looks like you have the event signature wrong. There should be a return value "As Boolean" and then, inside the event sub, you should specify a Boolean return value.
 

skeedsr

Member
hy everyone

i tryed the example in the first page but i recive this error while compiling

B4J version: 4.20 (1)
Parsing code. (0.00s)
Compiling code. (0.01s)
Compiling layouts code. (0.00s)
Compiling generated Java code. Error
B4J line: 8
NH.Initialize(\
javac 1.8.0_91
src\b4j\example\main.java:48: error: cannot access NativeKeyListener
_nh.Initialize("NH",main.getObject());
^
class file for org.jnativehook.keyboard.NativeKeyListener not found
1 error

some question

where i can find the file jnativehook 2.0.2 coz in the first page there is only 1.0.x

in my b4j i have not the folder named external library but i have Libraries and add libraries

thanks

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