B4J Tutorial Floating ContextMenu using jNativeHookB4j

This is an example of using @Roycefer 's jNativeHook to monitor all mouse events, even outside of the current app. It uses a ContextMenu to replicate the right click functionality. Default behaviour is overridden by the jNativeHook library and returning false from the relevant event sub.

It uses a dummy form to enable the context menu to be placed anywhere on the screen.

It's not been comprehensively tested and I can't guarantee it will work on Mac or Linux, but would be interested to know if it does.

Get the jNativehook files from here: https://www.b4x.com/android/forum/threads/jnativehookb4j-for-intercepting-system-input-events.55826/

Hope you enjoy it.

V 0.2 Update for Mac - see post #3
V 0.6 implements Tags, css styling of menu, checkmenuitem, submenus and a menutitle
V 0.7 Close all menus correctly
V 0.8 Added :
  • Ignore clicks on window (allows window to operate normally)
  • Multiple Floating Menus (Only one can be active at a time)
This update contains breaking code, MainForm has to be passed to the NativeHook initialization.

No version increment update fixed menu resize issue.
Demo only update to V0.8
V0.8.5 Added
ThRuST's latest css styles
Pause and Restart Mouse Listener
cmenu.css.zip contains the latest css.file
Tags : B4j Native Hook Floating Context Menu
12
 

Attachments

  • FloatingContextMenu-0-7.zip
    7.1 KB · Views: 573
  • cmenu.css.zip
    1 KB · Views: 580
  • FloatingContextMenu-0-8-6.zip
    10.3 KB · Views: 628
  • Test - cmenu.css.zip
    1 KB · Views: 528
  • FCMTest1.zip
    10.3 KB · Views: 561
Last edited:

ThRuST

Well-Known Member
Licensed User
Longtime User
AquaFX on my PC. Menubar (hovering over Help), label, button, ComboBox and RadioButton.

aquafx.PNG
 

stevel05

Expert
Licensed User
Longtime User
I don't use a Mac so I can't tell if it's any good or not.

I've added the css file separately to the first post. I have a feeling it may get more changes than the code.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Will controls look like this on Mac only? I don't fully understand how to use AquaFX. I was thinking it was used to support only CSS.
It changes the layout of the controls on every system then, I assume?
 

stevel05

Expert
Licensed User
Longtime User
Yes, all of the controls styling are based on a css file, for Javafx 8 the default is the Modena.css. This just modifies or maybe replaces that with some Mac type stylings. It says it won't work for all view on all PC's, so you would probably wan't to selectively load it, only if it's running on a Mac.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
This is what it looks like on my Mac. So all I have to do is to include the reference to the #AdditionalJar and only run the selected code if O/S is a mac?
Will it then display the CSS themes correctly on a mac?

B4X:
Dim AJO As JavaObject
    AJO.InitializeStatic("com.aquafx_project.AquaFx")
    AJO.RunMethod("style",Null)


AquaFX v0.1 on Mac

Aquamac2.jpg
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Steve, I tested your code with Aqua FX v0.1. This is what it looks like on my PC. Will give it a try on mac as well.

aquaPCtest.jpg
 

stevel05

Expert
Licensed User
Longtime User
That I don't know. It may help. But then again.

Just add it to the floating menus app and give it a try.
 

stevel05

Expert
Licensed User
Longtime User
You beat me to it.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
AquaFX with ContextMenu looks like like on PC, but a bit different when the mainform is not in focus. Those blue lines is part of the layout even on Mac when it's focused. I did not include the broadway font in the files folder, so maybe that will do it.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I have added the boardway TTF font to the files folder in contextmenu v085, is there all there is to it? will B4J make use of it perhaps? let's try.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
It did not work to just include the font in the files manager and run it on a Mac without AquaFX. I will try to enable AquaFX again I'm on it again. gosh :confused:
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I found an example for B4A how to use custom fonts. That might work in B4J

B4X:
Dim Sonda As Typeface
Sonda = Typeface.LoadFromAssets("wurm.ttf")
Dim dialog As CustomDialog
Dim lb As Label
lb.Initialize(dialog)
lb.Typeface = Sonda
lb.TextSize = 16
lb.Text = "This is my text what I write for B4A forum." _
&CRLF& "Ho Ho Ho Ho" _
&CRLF& CRLF& _
"Today is a beautiful day here in Croatia :)"

dialog.AddView(lb, 0dip, 0dip, 100%x, 100%y)
dialog.Show("My Title", "OK", "Cancel", "No", LoadBitmap(File.DirAssets, "nfo.png"))
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Actually in B4J it should be

B4X:
Private myFont As Font=fx.LoadFont(File.DirAssets,"BROADW.TTF", 15)
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Thanks for telling me, but this was also part of an important test. I installed the font via Font Book in my Mac and then I disabled AquaFX and compiled your code (v085) and ran it on my Mac. This is what it looks like. It can be a good idea to find nice fonts that can be used on any computer without violating the copyright, or there will be a royalty fee to pay which is not really needed since there's many nice fonts to choose from. If you find some you like let me know. Since it runs without AquaFX, I see no need to include support for it, if it's not for other systems I dunno.

MacOK.jpg

Looks pretty darn good if you ask me :cool: Been here done dat
 

stevel05

Expert
Licensed User
Longtime User
Yes looks good, so had that fixed the highlighting issue?
 

ThRuST

Well-Known Member
Licensed User
Longtime User
No, I just tested it and it's the same. Those menus look nice but the selections is not active. However it works as it should so it's not really a problem, even though it would be nice to have it working just like on Windows. So just one 'bug' to correct to make it v1.0 :) This might not really be a bug, since I'm pushing CSS too far.
It would be nice with some colors and stuff since CSS invites for it so I really want to use it. Possibly on every platform. Did you set focus to it?
 
Top