B4J Question jNativeHook v2.1 Library Update

ThRuST

Well-Known Member
Licensed User
Longtime User
I am using jNativeHook library posted by Roycefer in 2015. In his post here it says that it targets v2.2 but in the B4J v8.90 Libs manager it shows

1621884767614.png


I have these library files stored in the Additional libraries folder.

1621885319008.png


v1.10 still shows an error message in the log for Java 8 when exiting the application (like it did years ago)!
Please post the v2.2 jar and xml files / library updates / correct the exiting error / make library source available ?

V1.2 uses jNativeHook.jar version 2.0.2 which is included in the lib files. I share my update here.
Place it in the Additional libraries folder.
 

Attachments

  • B4J jNativeHook v1.2 (Compatible with jNativeHook v2.0.2.jar).zip
    194.2 KB · Views: 223
Last edited:

ThRuST

Well-Known Member
Licensed User
Longtime User
Here's the content in my jNativeHookB4J.xml file


B4X:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <doclet-version-NOT-library-version>1.04</doclet-version-NOT-library-version>
    <class>
        <name>butt.droid.nativeHook.JavaNativeKeyEvent</name>
        <shortname>NativeKeyEvent</shortname>
        <owner>process</owner>
        <property>
            <name>KeyText</name>
            <returntype>java.lang.String</returntype>
            <comment></comment>
        </property>
        <property>
            <name>KeyCode</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
    </class>
    <class>
        <name>butt.droid.nativeHook.JavaNativeMouseEvent</name>
        <shortname>NativeMouseEvent</shortname>
        <owner>process</owner>
        <property>
            <name>ClickCount</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
        <property>
            <name>X</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
        <property>
            <name>Y</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
        <property>
            <name>MouseButton</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
    </class>
    <class>
        <name>butt.droid.nativeHook.JavaNativeMouseWheelEvent</name>
        <shortname>NativeMouseWheelEvent</shortname>
        <owner>process</owner>
        <property>
            <name>WheelRotation</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
        <property>
            <name>ScrollType</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
        <property>
            <name>ScrollAmount</name>
            <returntype>int</returntype>
            <comment></comment>
        </property>
    </class>
    <class>
        <name>butt.droid.nativeHook.NativeHook</name>
        <shortname>NativeHook</shortname>
        <owner>process</owner>
        <event>NativeKeyPressed(nke as NativeKeyEvent) As Boolean</event>
        <event>NativeKeyReleased(nke as NativeKeyEvent) As Boolean</event>
        <event>NativeMouseMoved(nme as NativeMouseEvent) As Boolean</event>
        <event>NativeMouseWheelMoved(nmwe as NativeMouseWheelEvent) As Boolean</event>
        <event>NativeMouseClicked(nme as NativeMouseEvent) As Boolean</event>
        <event>NativeMouseDragged(nme as NativeMouseEvent) As Boolean</event>
        <event>NativeMousePressed(nme as NativeMouseEvent) As Boolean</event>
        <event>NativeMouseReleased(nme as NativeMouseEvent) As Boolean</event>
        <event>Unregistered</event>
        <method>
            <name>startNativeMouseWheelListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>endNativeMouseMotionListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>Initialize</name>
            <comment>Registers the NativeHook. Returns a reference to the Me jNativeHook Object. Starts the NativeHook event loop.
Be sure to unregister the NativeHook when done using the NativeHook by calling unregisterNativeHook().
Will throw a NativeHookException if it can't be registered.
Next, start the individual NativeListeners you require.
If you wish to consume event, call EnableEventConsumption().
If you want to filter MouseMoved events, call setMouseMovedFilter().</comment>
            <returntype>butt.droid.nativeHook.NativeHook</returntype>
            <parameter>
                <name>EventName</name>
                <type>java.lang.String</type>
            </parameter>
            <parameter>
                <name>Module</name>
                <type>java.lang.Object</type>
            </parameter>
        </method>
        <method>
            <name>disableKillCode</name>
            <comment>If you press left_windows_key and then Escape, and the KillCode is active,
the NativeHook will unregister and all event listeners will end.
Call this Sub to disable use of this KillCode.</comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>EnableEventConsumption</name>
            <comment>Call this to enable consuming events. If you consume an event, it will not propagate through
the system to other programs, including the program in focus. In order to consume an event
from with that event's event sub, return True in that sub.</comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>endNativeKeyListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>startNativeMouseMotionListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>isSpecialKey</name>
            <comment>Returns true if the key can be held down in conjunction with other keys for special key combos
(for example, alt, ctrl, windows key, shift, etc...).</comment>
            <returntype>boolean</returntype>
            <parameter>
                <name>keyCode</name>
                <type>int</type>
            </parameter>
        </method>
        <method>
            <name>keyCodeToTextRepresentation</name>
            <comment>Returns the text representation of a given keyCode</comment>
            <returntype>java.lang.String</returntype>
            <parameter>
                <name>keyCode</name>
                <type>int</type>
            </parameter>
        </method>
        <method>
            <name>enableKillCode</name>
            <comment>If you press left_windows_key and then Escape, and the KillCode is active,
the NativeHook will unregister and all event listeners will end.
Call this Sub to enable use of this KillCode.
The KillCode is disabled by default.</comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>startNativeKeyListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>startNativeMouseInputListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>isCharacter</name>
            <comment>Returns true if the primary function of the key with the corresponding keyCode is a letter, number or punctuation mark.</comment>
            <returntype>boolean</returntype>
            <parameter>
                <name>keyCode</name>
                <type>int</type>
            </parameter>
        </method>
        <method>
            <name>unregisterNativeHook</name>
            <comment>Unregisters the NativeHook. Ends the NativeHook event loop.
Throws a NativeHookException if unregistering fails.
Throws other exceptions if it fails to raise the _Unregistered event.
If the NativeHook is the only event loop (for example, no UI event loop, no server event loop, etc...)
then unregistering the NativeHook will end the program (no more events, no more code to execute) unless you
implement the _Unregistered event and continue execution from there.</comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>isKillCodeEnabled</name>
            <comment>If you press left_windows_key and then Escape, and the KillCode is active,
the NativeHook will unregister and all event listeners will end.
Call this Sub to get the activation status of the KillCode.</comment>
            <returntype>boolean</returntype>
        </method>
        <method>
            <name>endNativeMouseWheelListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <method>
            <name>endNativeMouseInputListener</name>
            <comment></comment>
            <returntype>void</returntype>
        </method>
        <property>
            <name>MouseMovedFilter</name>
            <returntype>int</returntype>
            <parameter>
                <name>mouseMovedFilter</name>
                <type>int</type>
            </parameter>
            <comment>On modern systems, MouseMoved events might fire more often than is convenient.
Setting mouseMovedFilter to N will make it so that only 1 in N MouseMoved events is reported.
By default, mouseMovedFilter=1 so that all events are reported.
This filtering also applies to MouseDragged events.
NOTE: If you are consuming MouseMoved events and you have set mouseMovedFilter to something
other than 1, you will only be able to consume the events that are reported.
If you need to be able to consume all MouseMoved events, keep filtering at 1.</comment>
        </property>
    </class>
    <version>1.1</version>
    <dependsOn>JNativeHook</dependsOn>
</root>
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@DonManfred Thanks, I will try that. Somehow I downloaded the file (from 'code' top right) at first, but I noticed the other file below on right side which contains the Jar file.
I believed that Roycefer maintained the source code to the jNativeHook library and since his latest post is from 2017 I wanted to put some light to this great lib.


1621949237082.png


The Jar file can be downloaded from here

Webpage looks like this
1621949353313.png
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I renamed the new Jar file to jNativeHook.jar and JNativeHookB4J.jar and updated the version to 2.1 in the xml file and this happened.
Obviously I made a misstake. (They are located in the additional libraries folder).
Btw there's no reference to #AdditionalJar: because it were not needed before the update.

1621950727398.png
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Only jNativeHook.jar should be changed since jNativeHookB4J.jar and jNativeHook.xml is part of Roycefer's source code.
That makes the library work with version 2.1
I have prepared an update in my first post. Roycefer might want to look at it someday.. :)?
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@DonManfred Indeed so. I should refer to Roycefer's original post which can be found here
He states that the current B4J jNativeHook library targets v2.0.2 so I should better include that in my first post.
That explains why any error messages in the log might araise because I used v1.1 while the his library targets v2.0.2 .
Hopefully Roycefer will look into this since v2.1 is available on BugHub ;) ?
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
This solved it.
I will put up the zip in my first post so all of you can use this great library will full compatibility (v2.0.2) as of May 2021.
@Roycefer Please look into updating your library or share the source code here or in your original post.

Link to the page here
1621953333512.png


Log
1621953294526.png


Mission complete :)?


- or maybe not yet!! My version said v1.1, anyone out there who has a never version? Might be good to ask before spreading an older update around..
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@agraham Yep, but I've sorted this out. V1.2 is compatible with the jar file that Roycefer recommended.
It's included in the zip. Worth mentioning is that that the keycombo in the version I used (v1.1) used names like CTRL
instead of command, which I've seen in other posts. jNativeHook.jar v2.0.2 is compatible with this initialization


B4X:
kcl1.Initialize(Me, "kcl1", Array("control", "shift")).StartListening

Which is pretty cool. Which means I can implement shift+ctrl in my project (as my test on Win worked). Hopefully this will work better on Mac as well.

Unless there's a never version posted anywhere in this community you can use the version I put together v1.2. Attached in Post #1

EDIT: This works well on MacOS El Capitan as well (someone is happy)
Enjoy :)
 
Last edited:
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
when try i get
B4X:
NH.Initialize(\
src\b4j\example\main.java:101: error: cannot access NativeKeyListener
_nh.Initialize("NH",main.getObject());
   ^
  class file for org.jnativehook.keyboard.NativeKeyListener not found
any suggetion
 
Upvote 0

teddybear

Well-Known Member
Licensed User
when try i get
B4X:
NH.Initialize(\
src\b4j\example\main.java:101: error: cannot access NativeKeyListener
_nh.Initialize("NH",main.getObject());
   ^
  class file for org.jnativehook.keyboard.NativeKeyListener not found
any suggetion
You‘d better create a new thread for your question.
It should be you are using a wrong version of the JNativeHook.jar
 
Upvote 0
Top