Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 1470 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
    case KeyEvent.ALT_MASK: // Handles Alt key

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

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!
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):
Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (159 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!