Getting mouse/keyboard events? and position

How would I get the mouse position and keyboard events? I chose JInput because someone said that it can detect keypresses/keyreleases straight from the hardware, and AWT only detects keypresses/keyreleases when the AWT window is the one on top…

I’ve tried some code…


public class ControllerHandler {
	private static Controller mouse = null;
	private static Controller keyboard = null;
	
	public static void main(String args[]) {
		Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
		for (Controller controller : ca) {
			if (controller.getType() == Type.MOUSE) {
				mouse = controller;
			} else if (controller.getType() == Type.KEYBOARD) {
				keyboard = controller;
			}
		}
		if (mouse == null || keyboard == null) {
			throw new NullPointerException("You either don't have a keyboard or mouse..");
		}
		while (true) {
			mouse.poll();
			EventQueue queue = mouse.getEventQueue();
			Event event = new Event();
			while(queue.getNextEvent(event)) {
			
			}
		}
	}
}

Yeah… help is appreciated! ;D

Also, is it possible to use JInput to call AWT events? so if Jinput detects an event it can call keyPressed/keyReleased/etc on AWT?

Hi

JInput talks to OS APIs. Each OS has it’s own rules on what an application can and can’t do. On Linux, before xinput-evdev broke the world, the mouse and keyboard events would be detected no matter what windows had focus. On windows we have 2 plugins, one uses directx, one raw input. Raw input is used for mouse and keyboard as it supports multiple mice and keyboards, but it does only detect them when one of the applications windows has focus. Directx is used for the joysticks. Directx does allow reading a device when a window does not have focus. On windows you can set up a propertie to tell it to not use the standard plugin, and then one to tell it to use the directx plugin

-Djinput.useDefaultPlugin=false -Djinput.plugins=net.java.games.input.DirectInputEnvironmentPlugin

JInput is designed to get the relative position of the mouse, so it’s only movement. That is what the mouse does. The cursor has a screen position, a mouse doesn’t. That means that JInput does not know about screen positions.

HTH

Endolf

is there a function to get the position of the mouse and keyboard/mouse button presses?

also, will


System.setProperty("jinput.useDefaultPlugin", "false");
System.setProperty("jinput.plugins", "net.java.games.input.DirectInputEnvironmentPlugin");

do the job? I don’t want to use a .bat file.

Hi

[quote]JInput is designed to get the relative position of the mouse, so it’s only movement. That is what the mouse does. The cursor has a screen position, a mouse doesn’t. That means that JInput does not know about screen positions.
[/quote]
Not in JInput, you can from AWT. I think LWJGL bypass JInput for keyboard/mouse and talk direct to the OS, I don’t know if they expose cursor locations.

You can set the properties any way you like, as long as it’s done before the JInput classes are loaded.

HTH

Endolf

Well, really all I need JInput for is reading keys directly from the hardware, because On AWT if you hold a key and click outside the window then release the key, the system will keep generating KeyPressed events. That could be used for botting. Is there a way for Jinput to detect whether a key is pressed/released outside the AWT window?

Can you read keys from JInput? If so can I have an example? Thanks.

Yes

To see how to use it, try this thread.

HTH

Endolf