Results 1 to 1 of 1
Thread: Key Events
- 04-15-2011, 09:02 AM #1
Key Events
I came across a few interesting problems with a few other people about key events and how to code this functionality with games smoothly and effectively. Im sure there are many ways of being able to process these events but i wrote a class and an interface that helps and works great.
public interface Event {
public void perform();
}
public class Key {
private boolean isPressed;
private int keyCode;
private Event event;
public Key(int keyCode, Event event){
this.keyCode = keyCode;
isPressed = false;
this.event = event;
}
public int keyCode(){
return keyCode;
}
public boolean isPressed() {
return isPressed;
}
public boolean pressed(boolean isPressed) {
return this.isPressed = isPressed;
}
public void perform(KeyEvent e){
if (e.getKeyCode() == keyCode)
event.perform();
}
}
ctrl = new Key(KeyEvent.VK_CONTROL, new Event(){
public void perform() {
if (ctrl.isPressed())
ctrl.pressed(false);
else
ctrl.pressed(true);
}
});
public class KeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
ctrl.perform(e);
}
public void keyReleased(KeyEvent e){
ctrl.perform(e);
}
}
I have other code that works just as well, but when i write my code i like to write it as if anyone can re-use it for any case or solution.
Similar Threads
-
typed events vs untyped events.
By Drun in forum SWT / JFaceReplies: 0Last Post: 11-23-2009, 01:22 PM -
Mouse events, are they best or only way to go?
By dbashby in forum New To JavaReplies: 2Last Post: 04-10-2009, 05:34 PM -
Need Help with events
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 09-23-2008, 04:18 AM -
swing events
By chitra in forum AWT / SwingReplies: 3Last Post: 09-20-2008, 05:57 PM
Bookmarks