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: 480
  • cmenu.css.zip
    1 KB · Views: 475
  • FloatingContextMenu-0-8-6.zip
    10.3 KB · Views: 527
  • Test - cmenu.css.zip
    1 KB · Views: 449
  • FCMTest1.zip
    10.3 KB · Views: 455
Last edited:

stevel05

Expert
Licensed User
Longtime User
(that replaces the one showing)

I'm not sure what you mean by this. If you click a button on the window, the context menu will close.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I mean if you add more buttons in the form that enables the menu, the first one shows a single contextmenu options, but if you add another button to the form, FM3 then the contextmenu with a main menu options including with a submenu can be accessible, which allows to choose which menu to use and when. My new CSS templates will crush all competition. We're unbeatable together :)
 

stevel05

Expert
Licensed User
Longtime User
Demo Changed V0.8
 

stevel05

Expert
Licensed User
Longtime User
Do you mean edit an existing menu? It would be simpler just to have a duplicate with the additional options added.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I guess there's no need for submenus because of all the buttons on the mouse. You're right, it will also be easier to use it this way. The new design is really nice. There's just much tweaking to get it right. I'm editing the script while the code runs, in case you didn't know this. The first five buttons can be called from int 1-5 while the rest of the buttons (6-9) used keyboard combinations. These can easily be set in the tray application provided by in this case Logitech, which means jAWTRobot must be used to made such calls. I assume you read about it yesterday so you know how it works. I dunno which keyboard combinations is appropriate to use. Those should be available and not be in conflict with any other software. They should also work on both Windows and Mac. You should perhaps buy the mouse to get familiar with it. This is the Swedish version so you'll not be able to read it, but it's easy to get an idea how it's used. The changes is stored inside the mouse :eek:

screentest1.PNG
 

ThRuST

Well-Known Member
Licensed User
Longtime User
btw there's drivers for both PC and Mac for these kinds of gaming mouses, and Logitech also provides SDKs to set the led color and stuff. This could be a cool feature to change led color in the mouse depending on which menu is showing, but that's not really important in this case.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Code allways changes. You just have to make it work before each update :)
 

stevel05

Expert
Licensed User
Longtime User
You should also implement keyboard shortcut alternatives for all options for people that don't have one of these mice. Perhaps try using Ctrl+Alt+NumPad key as default as there are 9 of them.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I forgot this, about the logitech tray application. Any combination can be programmed in one, two or up to three combinations. So if using a 3 way combination it should not conflict with any other software. At least the chance are lower that to does. if this can be assigned to the AWTRobot then that will be awesome :)
 

ThRuST

Well-Known Member
Licensed User
Longtime User
That is a great idea. Funny, I was thinking that you was to inbed the code for AWTRobot. But then I realize that that's my w:eek:rk. Or if you add it that'll be great ;)
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I just noticed that left and right buttons cannot be assigned to a keyboard shortcut in Logitechs application. They can only be shifted with eachother. The middle mousebutton (mousewheel) and the rest can be assigned a keyboard shortcut. I think this won't inflict any problems when it's possible to access all menus through button shortcuts. Besides users can use their own mice. I said Mouse and you mice!! Maybe I was refering to that of a rat, but I guess you understood anyway :)
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Steve, I just tested you update to v08 and it's really nice. Now it's starting to look really useful. This makes me impressed how well you implement the functionality based on our conversation. Your code together with my CSS creates a perfect balance. This is a good example how efficient it is to work as a team. Much much powerful than single development efforts, that usually ends up in nowhere.
 

stevel05

Expert
Licensed User
Longtime User
I think most people will have a 2 button mouse with scroll wheel, not sure about Mac or laptops though. I think most touchpads on Laptops can emulate a scrollwheel these days. Should be OK.

Mouse, mice? just plural, not sure if it should apply to hardware but it sounds right.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
I think the markers subitems looked better without the box!!! why add that? it looks better without it because the box is too close to the menuitem text, but if you add a space to it it's better. READ MY LIPS :D
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Steve, Good news.. It works PERFECTLY on my MacBook. The contextmenu looks like I'm running it on Windows even though it's OSX El Capitan. Awesome :)
 

stevel05

Expert
Licensed User
Longtime User
So do you have to make an Aqua theme as well?
 

ThRuST

Well-Known Member
Licensed User
Longtime User
You want a transparent CSS theme? I have not yet looked that up but I assume it could be done. Nice idea I'll look it up.
 

stevel05

Expert
Licensed User
Longtime User
A thought Aqua was the Mac lookalike theme. :)
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Not sure what you mean, but the contextmenu used the CSS that comes with the code. It is not affected by the menu design used in OSX El Capitan.
Possibly this can be changed, but I'm not sure how. I am working on a new layout, similar to the dark theme I posted earlier. But this will be compatible with v08.
After that you updated your code there's no problem to create complex CSS themes. In fact it works flawlessly. I recommend that you use the theme I will post since it works very well. I will perhaps comment each line to make it easier to locate what each block does. I dunno about you, but I'm not an CSS expert.
I just keep changing stuff untill it works, that's why it's a rather time consuming task. But once it works it's worth every effort (and time too) :rolleyes:
 
Top