• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pressing multiple KeyEvents at once

 
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I am currently developing a small game. I want the sprite to be able to move right and upwards at the same time. However, currently it only moves either right or up (pressing right makes it go right, then pressing up simoustanly makes it stop move right). How do I make the sprite move both up and right if both keys are pressed?

I have tried the following, but it gives an error when I remove the last element in the arraylist (by releasing the button).

Two questions:
1) How do I make a sprite move up and right simoustanly?
2) Why does it give me an error when removing an element in the arraylist?



 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post a small, simple program that compiles, executes and shows the problem?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The remove method you call isn't the one you think it is. You think that, because you can call add with the key code as its argument, you can call remove with the same argument and it will be removed. That would work with a Set, but List adds an overloaded remove method that takes an int index. Because the key code is an int, this is the method you're calling. So instead of removing the key code element, it's using the key code as the index of the element to remove.

Simple solutions:
1) Switch to Set: Set<Integer> buttonsPressed = new HashSet<>();
2) Explicitly tell the compiler to use the remove(Object) method by casting the key code when removing: buttonsPressed.remove((Object) e.getKeyCode());
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

overloaded remove method that takes an int index. Because the key code is an int, this is the method you're calling. So instead of removing the key code element, it's using the key code as the index of the element to remove.  


That has caught me before.  add(an int value) and remove(an int value) are not symmetrical because of autoboxing.
If the keycode values are larger than the size of the List, there should be an IndexOutOfBoundsException - if the index is out of range
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also need to be aware that, on many platforms, holding down a key may cause repeated KeyPressed events after a short delay, with no intervening KeyReleased event. So when you get a KeyReleased event, removing one entry in your buttonsPressed list will be insufficient - you need to remove all of the entries for that key.

One way of fixing this is to check whether that key is already in the list before adding it (in your keyPressed() method). Another way is to use a collection that only allows one entry for a particular key, such as a Hashmap or TreeMap.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:You also need to be aware that, on many platforms, holding down a key may cause repeated KeyPressed events after a short delay, with no intervening KeyReleased event.


Are you sure it's the KeyPressed event that is repeated, and not the KeyTyped event?

Another way is to use a collection that only allows one entry for a particular key, such as a Hashmap or TreeMap.


Why a Map and not a Set, like I've already shown? I'd go for HashMap, since TreeMap is mostly useful if you need ordering or a custom equality determination (using a comparator, e.g. case insensitive).
 
reply
    Bookmark Topic Watch Topic
  • New Topic