Keyboard event handling strategy when combining Swing + jME/InputManager

I have a Swing gui working alongside jME fairly well, with one persisting issue. I currently handle keypress events using jME’s InputManager. I prefer to keep using this system to handle events as much as possible, particularly keypress events which effect the behavior of the jME portion of the app. (I prefer jME handling of events for a few reasons.)



The issue I’m having is that when you click on a Swing component, keyboard events for doing things such as scrolling the camera view (WASD) do not make it to InputManager until the mouse returns focus to the jME canvas via a click. I’d like certain keyboard events to occur in jME regardless of focus (with the exception of text input fields in Swing). I’ve tried disabling focus for the Swing components I’m using, but the behavior hasn’t changed (I don’t think I’ll need Swing to hold focus).



How should I be approaching this integration issue?

These mouse clicks are simply nonexistant for jme, you have to use the swing/awt mouse data for this and send it to the update loop in a callable.

You can use AwtKeyInput/AwtMouseInput, you will need to reinitialize InputManager to do so however

I’ll take a look at doing this and see how it goes, thanks.

Yeah, this isn’t as straightforward as it sounds. If I understand correctly that I need to re-instantiate (and replace) the InputManager created in Application.initInput(), it’s just not working as I’d hoped no matter what AWT Components I pass to the new Awt*Input instances. Key events aren’t working at all and mouse events (move/click only, wheel not working) only get to jME when the mouse is hovering over the Swing/AWT panels (which jME needs to ignore). And when the mouse is over the jME canvas itself, it’s not capturing any events at all. This has me wondering whether I should really be using Swing for this in the first place. I never cared for the additional complexities anyway.



Still open to suggestions.

Yeah it is not straightforward. The jME3 input system really was not designed for this.

Although to me it seems you would only want to get the events if the canvas is in focus anyway, no?

For the most part yes, but I want the user to be able to scroll the camera on the canvas via WASD, and also via mouse wheel inc/dec for camera height. Also some shortcut/hotkeys such as F1, Tab, etc should function regardless of whether the jME canvas is in focus or not.



And jME’s input handling is much better for things like scrolling (for a “non productivity app”) - so if I were to just forward AWT events, the operating system’s built-in keypress repeat delay makes scrolling a frustrating experience.



The Swing UI isn’t terribly large, actually I don’t mind just doing this at the end of all my Swing ActionListener handlers:

[java]

JmeCanvasContext ctx = (JmeCanvasContext) bme.getContext();

ctx.getCanvas().requestFocusInWindow();

[/java]



Works in one case, so I think I’m going to just try doing this when needed so I can get this project done. Later maybe I’ll look into contributing to NiftyGUI or maybe even getting TWL to work with jME3.