Firing key events manually

Hi,
Is there any way to fire key events manually, outside the KeyInput code? I tried InputManager.onKeyEvent(), but it doesn’t work (UnsupportedOperationException(“KeyInput has raised an event at an illegal time.”)). I had an idea to modify the keys InputManager field, but it’s private
So, my question is: is it even possible to fire key events?

Thanks in advance,
Regards.

I don’t think its possible. Just factor out ur event code into a function and call that directly.

@wezrule said: I don't think its possible. Just factor out ur event code into a function and call that directly.
How? To do it, I would have to modify some methods in InputManager class. To modify any method in InputManager class, I need to re-write it, because all fields in this class are private, so I don't have any access to them. I cannot also add to my subclass of InputManager any methods that use that private fields. The only way to do something is to re-write whole class...

I’ll try with writing own Input and modifying initInput() method in Application class (which is also private, so I’ll have to re-write 2 other methods in this class…).

EDIT:
Wow, more privates that I previously thought. :slight_smile: Application.java:
[java] public void initialize(){
if (assetManager == null){
initAssetManager(); // private
}

    initDisplay(); // private
    initCamera(); // private

    if (inputEnabled){
        initInput(); // private, re-implementing it by myself
    }
    initAudio(); // private

    // update timer so that the next delta is not too large

// timer.update();
timer.reset();

    // user code here..
}[/java]

It seems that the simplest way is to do what I want is to call super.initialize() normally and then create InputManager second time… :slight_smile:

<p>EDIT2:
Am I doing something wrong here? The code doesn’t work. update() method is called, but there’s no effect after all (the FlyByCam isn’t moving…).
[java] keyInput = new KeyInput() {
protected RawInputListener listener;

		@Override
		public void update() {
			KeyInputEvent evt = new KeyInputEvent(KEY_W, 'w', true, false);
			evt.setTime(System.nanoTime());
			listener.onKeyEvent(evt);
		}

		@Override
		public void setInputListener(RawInputListener listener) {
			this.listener = listener;
		}

		@Override
		public boolean isInitialized() {
			return true;
		}

		@Override
		public void initialize() {
		}

		@Override
		public long getInputTimeNanos() {
			return System.nanoTime();
		}

		@Override
		public void destroy() {
		}
	};[/java]

<p>EDIT3:
Yeah, now it works.
[java]KeyInputEvent evt = new KeyInputEvent(KEY_W, ‘w’, new Random().nextBoolean(), false);[/java]
There’s a bit of random in it.

@m4tx said: Hi, Is there any way to fire key events manually, outside the KeyInput code? I tried InputManager.onKeyEvent(), but it doesn't work (UnsupportedOperationException("KeyInput has raised an event at an illegal time.")). I had an idea to modify the keys InputManager field, but it's private... So, my question is: is it even possible to fire key events?

Thanks in advance,
Regards.

No, it’s not possible because it’s unnecessary. Can you tell us why you want to do this? What’s the use case?

…then we can better tell you how to really do what you want.

I’m writing a map editor and I combine SWT and jMonkeyEngine in one window using the SWT_AWT bridge. Unfortunately, default JmeCanvas causes really, really annoying glitches. When you have focus on canvas, you can’t use hotkeys defined in window (and vice versa - when you have focus on window, you can’t move on scene). Also, it causes some random blinking of the window and glitches in focus in whole window manager (but I think it’s Linux/X server/something && SWT bug; anyway - it’s really annoying).
So I decided to make JmeCanvas unfocusable. That also means that default LwjglKeyInput stopped working. That’s why I must find a solution.

But…

InputManager would only be calling listeners that you registered in the first place.

And…

Somehow you have to register for key events in SWT to forward them to JME anyway.

So…

Why can’t you just register listeners with SWT and avoid the InputManager?

InputManager’s job is to take hardware input and send it to your registered listeners. If you want to invoke your listeners manually then just invoke them manually… InputManager doesn’t necessarily need to be involved.

@pspeed said: Why can't you just register listeners with SWT and avoid the InputManager?
I can, FlyByCam can't ;>

EDIT:
Hm, but FlyByCamera isn’t fully-private like other classes like InputManager… So it seems that I’ll code your solution, @pspeed. Thanks!

@m4tx said: I can, FlyByCam can't ;>

EDIT:
Hm, but FlyByCamera isn’t fully-private like other classes like InputManager… So it seems that I’ll code your solution, @pspeed. Thanks!

Also, FlyByCamera’s only job is to hook InputManager up to a Camera. It’s a trivial class to duplicate for other cases… and there are benefits to that too. It’s kind of an ugly implementation so I expect/hope that most projects reach a point where they write their own camera manager anyway.

Good luck! :slight_smile: