Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Is it possible to read 2 keys at the same time ?  (Read 864 times)
0 Members and 1 Guest are viewing this topic.
Offline falkmar

Innocent Bystander





« Posted 2006-05-24 15:45:42 »

I want to write an applet-game.
And I use this pieced of source:

public boolean keyDown(Event e, int key)
 {
    if (key == Event.UP) up();
     if (key == Event.DOWN)  down();
     if (key == Event.LEFT) left();                     
     if (key == Event.RIGHT) right();
     if (key == Event.ENTER)  shot();
}

But if I press enter and a cursor key together only the shot() or the other function is called.
Want can I do ?



for the control but, it
    

Offline gatorbyte

Innocent Bystander





« Reply #1 - Posted 2006-05-24 21:14:22 »

With the way computers are architected, it is not possible to accept more than one key from the keyboard at a time, unless the other key is what is called a modifier in Java, such as Ctrl, Alt, or Meta (in the case of space cadet/devorak keyboards), or Command, or Open Apple in the case of Macintoshes.  It's just the way things are built.  A lot of games take this to their advantage though, and use the Ctrl key for shooting while the arrow keys control movement.  Other games take advantage of using the mouse for movement and then the keyboard for other things, such as special commands, shooting, jumping, and so on.  When "listening" for keys in a game loop/event listener, I would suggest always using the keyPressed/keyReleased handlers as opposed to the keyTyped (or KeyDown/KeyUp which wait until the other event is caught to release control, see my later comments about the keyboard in this post) handler, which only really listens for typable character keys (letters, numbers, and symbols) and use the KeyEvent.VK_*** constants to compare your key captures.  Here is an example:

public void keyPressed(KeyEvent ke) {
  int modifiers = ke.getModifiers();
  int keycode = ke.getKeyCode();

  switch(modifiers) {
    case KeyEvent.CTRL_MASK: // Handles Ctrl key
      break;
    case KeyEvent.ALT_MASK: // Handles Alt key
      break;
  }

  switch(keycode) {
    case KeyEvent.VK_LEFT: // Handles left arrow key
      break;
    case KeyEvent.VK_RIGHT: // Handles right arrow key
      break;
  }
}

Of course using a mouse listener you could just watch for changes in the x position of the cursor, which I did in one of my very first attempts to create a game in Java.  If you decide to use a joystick or control pad (which I don't know much about) then I believe that the button inputs are all handled separately, so that simultaneous button presses appear to be handled differently.  The problem with the keyboard is that it operates on a queue-like system (last in - first out) kind of (a queue is first in - first out) - actually if you're holding down a key, then press another key, the second key just takes over.  I hope that helps.  I had the same frustration when I was delving into QBASIC game programming many many years ago, but I'm grateful for the many other options that Java has implemented for us.  Another thing that you might look into is creating inertia, in other words, you could let the character keep moving on its own when you press another key on the keyboard that "takes over".  Just a thought, that I haven't really thought out.  The other stuff is probably more realistic.

Good luck!
Offline Jeff

JGO Coder




Got any cats?


« Reply #2 - Posted 2006-05-25 05:32:38 »

Actually it is possible to read multipel keys, with a few caveats...

(1) You need to go lower in the system, look at JIInput

(2) Be aware that on some computer systems, some key combinations cause "ghosting" which is the report of a "ghost key" you did not press.  This is beause of the matrix-scanning used to read the KB.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #3 - Posted 2006-05-25 17:13:33 »

I wrote an example of this a while ago (doesn't use JInput, though): http://woogley.net/misc/MultiKey/
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.

TehJavaDev (18 views)
2014-08-28 18:26:30

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

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

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

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

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

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

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

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

BurntPizza (38 views)
2014-08-08 02:01:56
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!