Skip to main content
gamedev.net
🔒 Locked

[java] Keyboard Key List

Started by cyrax256 5 replies 900+ views
Original Post
cyrax256
cyrax256
Hi, I want to give the player the power to change the layout of the game keys to his own liking (It´s a Tetris)... But the list of keys is very long,and doing it manually may cause that I forget to add every key, is there anybody who has done this before?, or a part in the API which does this automatically (I doubt it)? Thanks El Ciro
cyrax256
cyrax256
Of course!, that''s the idea!, it''ll have an options dialog which will show other things besides the key layout, but what I''m talking about is the list of keys itself :-), I mean: A, B, C, Up, Down, Left, Right, etc. etc.

El Ciro
CaptainJester
CaptainJester
Just have the dialog use the same keyListener as the one that you use to control the game. Then you can have some variables in that class that hold the values the user pressed.

  
public class GameClass implements KeyListener {
int UP;
int DOWN;
bool keys[8];
etc...

public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(the source of the event the key setting dialog) {
if(type="UP") {
UP=e.getKeyCode();
}
else if(type="DOWN") {
DOWN=e.getKeyCode();
}
}
else {
if(e.getKeyCode()==UP) {
keys[0]=true;
}
if(e.getKeyCode()==DOWN) {
keys[1]=true;
}
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==UP) {
keys[0]=false;
}
if(e.getKeyCode()==DOWN) {
keys[1]=false;
}
}
}


This is kinda messy, but it should give you an idea of what to do.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
cyrax256
cyrax256
My question was if there was a list avaiable of all the keys in the keyboard, so I don''t leave any key behind...

But nevermind, I''ll do it another way :-).

Thanks anyway
Melraidin
Melraidin
Why do you need to give them a list of all the keys on the keyboard? You''re going to have a list of all the keyboard functions that are available in your game (for tetris probably only 5), and then let them choose which one to redefine. They hit a key, you save the keycode, and then in-game test for that keycode.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.