Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Getting mouse/keyboard events? and position  (Read 3303 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Member




...


« Posted 2009-03-30 08:07:40 »

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..

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
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! Grin

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?
Offline endolf
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #1 - Posted 2009-03-30 08:58:12 »

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
1  
-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

Offline Renoria

Junior Member




...


« Reply #2 - Posted 2009-03-30 09:19:12 »

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

also, will

1  
2  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline endolf
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #3 - Posted 2009-03-30 20:21:15 »

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.
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

Offline Renoria

Junior Member




...


« Reply #4 - Posted 2009-03-31 12:50:51 »

Hi
 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.
Offline endolf
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #5 - Posted 2009-03-31 20:14:15 »

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
1  
-Djinput.useDefaultPlugin=false -Djinput.plugins=net.java.games.input.DirectInputEnvironmentPlugin


Yes

To see how to use it, try this thread.

HTH

Endolf

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (131 views)
2013-05-15 15:01:13

theagentd (119 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.142 seconds with 21 queries.