Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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 5002 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Member




...


« Posted 2009-03-30 06: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

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #1 - Posted 2009-03-30 06: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 07: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

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #3 - Posted 2009-03-30 18: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 10: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

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #5 - Posted 2009-03-31 18: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.

 

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

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (18 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (63 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!