Results 1 to 7 of 7
  1. #1
    abbeywell is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Multiple keyboard presses

    I am currently working on a multiplayer air hockey game which, if played by two human players from the same computer, requires the two paddles to be controlled from the same keyboard by each player (for instance).

    I have created two classes which extend the Behavior class, each controlling each two paddles. processStimulus() is called every time a key is pressed however, no two keys can be pressed at once, so the game is unplayable. Is there a way to allow multiple key presses?

    Thanks,

    AW

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    The general approach to this is to keep track of a boolean for each key you care about. On keyDown, set it to true, on keyUp, set it to false. Then in your game loop, check those booleans to perform the appropriate actions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    abbeywell is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    thank you,

    if anyone has the same problem, this may come in handy Help - Swing Timer, 2 KeyEvents

    SOLVED - please close

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can mark your thread solved with the thread tools at the top of the page.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    Quote Originally Posted by abbeywell View Post
    thank you,

    if anyone has the same problem, this may come in handy Help - Swing Timer, 2 KeyEvents

    SOLVED - please close
    Sorry, but that seems like a pretty crappy solution. Why on earth would you need a separate Timer for each key?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Be aware that you might run into hardware issues. I used to have a Microsoft keyboard on which, if more than two keys were pressed simultaneously, the release of keys after the second was not detected, simulating stuck keys.

  7. #7
    abbeywell is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Sorry, but that seems like a pretty crappy solution. Why on earth would you need a separate Timer for each key?
    I cant seem to get it working unless I use a different timer for each key. Could you be more elaborate?

Similar Threads

  1. Keyboard
    By UJJAL DHAR in forum New To Java
    Replies: 4
    Last Post: 01-29-2011, 02:26 PM
  2. getting each character from keyboard
    By Sreejesh25 in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 01:08 PM
  3. KeyBoard Problem
    By arimakidd in forum Java Applets
    Replies: 3
    Last Post: 09-19-2008, 04:05 PM
  4. Responding to button presses
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:38 PM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •