J2ME Combo key press (multiple keys at once) - java

I am using keyboard_key variable from here:
//overrides the function keyPressed from "lcdui.Canvas"
protected void keyPressed(int keyCode){
keyboard_key = keyCode;
}
to detect if any key was pressed on a mobile phone.
But it returns only the key that is pressed most lately and it doesn't tell if any other key might be pressed. Please help!
Btw, I'm using NetBeans 7.0.1 as IDE.

...it returns only the key that is pressed most lately and it doesn't tell if any other key might be pressed
The way you use it in your code snippet, keyboard_key will always contain only the key that is pressed most lately - just because it "has no room" to hold anything more than that.
Consider using Vector to "memorize" different keys that were pressed.
//define in your class:
Vector keysPressed = new Vector(); // to keep track of keys pressed
//overrides the function keyPressed from "lcdui.Canvas"
protected void keyPressed(int keyCode){
keysPressed.addElement(new Integer(keyCode));
}
Side note given the question, you may benefit from studying Java language basics. There are many tutorials available online - just search the web for something like "Java getting started".
Depending on your application requirements, consider overriding keyRepeated along with keyPressed.
If you intend to handle key presses in game-loop fashion ("multiple keys at once" suggest that you may possibly have this in mind), consider another option provided by lcdui.game.GameCanvas API, method getKeyStates():
Gets the states of the physical game keys. Each bit in the returned integer represents a specific key on the device. A key's bit will be 1 if the key is currently down or has been pressed at least once since the last time this method was called. The bit will be 0 if the key is currently up and has not been pressed at all since the last time this method was called. This latching behavior ensures that a rapid key press and release will always be caught by the game loop, regardless of how slowly the loop runs...

Related

glfwGetKeyName() returns null

I'm currently working on the input system of a game engine in java, and I use GLFW for the window. I set up a callback system to catch when a key is pressed:
GLFW.glfwSetKeyCallback(window, (windowId, key, scancode, action, mods) -> {
if (action == GLFW.GLFW_PRESS) {
System.out.println(GLFW.glfwGetKeyName(key, scancode));
}
});
And the problem is when I press space or enter or shift plus a different key it prints out null. My question is: how to use the mods attribute to capitalize the next key when I press shift or print out a new line when I press enter etc.
I don't know the solution for the first question, but the second one is easy.
Try using char callback. This one is different from key callback, as it only works with unicode characters being typed by user (letters are capitalized with Shift, CapsLock button also works). Also, if you want to detect modifier buttons being pressed while user types, try using charMods callback. Seems like the latter is marked for removal, so you can test whether the modifier button is pressed with glfwGetKey().
EDIT: Seems like I didn't understood you on the first one. glfwGetKeyName() is not designed to be used as text input method. Also note that it can display names for a limited range of buttons. What is more, when you press multiple keys, press event for each one is called in separate key callback event. You can't handle multiple key presses at once. (Modifier keys are put in mods argument of callback, but I can't tell by now whether they also trigger a key press event or not - check it yourself)

Userset Keybind For Java

So, I am making a application, when you click a button (jButton1) it prints "Hi" (for example)
Now, I also have a textbox. In the textbox you need to specify a key.
How do I make so when you press the key you specified, it runs jButton1.doClick()
UPDATE: This is a auto clicker. So, I have a boolean
started
If I type k (out of the application) I want it to set the boolean started to true. If I type k again, and boolean started is true, set it to false.
Thank you so much!
UPDATE 2: I really need help! Why won't String code = NativeKeyEvent.getKeyText(nativeEvent.getKeyCode());
if (code == AutoClickFrame.jTextField1.getText().toUpperCase())
{
System.out.println("Hello World!");
}
work?
How do I make so when you press the key you specified, it runs jButton1.doClick()
You use Key Bindings. That is you map a KeyStroke to an Action. When the key is pressed the Action is invoked. The Action would also be used as the ActionListener of the button.
Read the section from the Swing tutorial on How to Use Key Bindings for more information.
For a working example check out: Attaching A Single Action Listener To All Buttons

How can I just move only one element in a 2D-Matrix no matter how long I press a key?

I'm trying to make a simple applet game in java.
I work with the KeyDown method to move an object Up, Down, Left and Right in a 2D-Matrix. This works perfectly but if I press one of the keys down and don't release it, the object moves one field in the right direction, then freezes for some milli seconds and then keeps moving super fast in the same direction.
Is there a way I can press the key as long as I want but the object will move just one element in the Matrix?
You could add a down state to your listener:
on key down, if state is up, change state to down and handle event
on key up, if state is down, change state to up
ignore other events
this way, only the first keyPressed, which is the reall key down, will trigger your code
EDIT: I wanted to add something about the keyRepeats property I seemed to recall, but instead fould this question. It seems to be highly relevant, if yours is not even a duplicate
I am not exactly sure, which framework you use for displaying your applet and handling your Input, but usually KeyPressed is use for operations, which are performed only once after the actual 'key-down'.
Implement the KeyListener-interface. Just like:
public class MyApplet extends Applet implements KeyListener {
...
public void keyPressed(KeyEvent evt) {
//do stuff
}
...
}
Look here: javakode.com/applets/05-keyboardInput
and here: java.applet.Applet oracle-reference

multiple key detection for KeyListener (java)

How would one implement KeyListener so that I can create a two-player system where one person uses '.' and '/' to control a character, and the other person can use the arrow keys without them interrupting each other? The way I have it now is that when one person holds down the arrow key, their character moves, but the instant you use the other player's controls, the first person's character stops.
Create a HashMap<Int,Boolean> that marks which keys are currently pressed/depressed.
Then in your game loop, you can move your objects depending on if the keys are depressed in this map.
For example:
if (keyMap.get(VK_COLON) == Boolean.TRUE) //True indicates pressed
playerAXPos+= 10;
From the sounds of things you're listening to a keyPressed event. Basically, you need to maintain stateful information about what keys are currently "down" and only stop the appropriate action when the keyReleased event occurs.
This will require to have two seperate lines action handler, one for when the key is pressed and one when the key is released.
One of the other things you might need to do is maintain some kind of cache of the active keys...which just got mentioned by Ethan as I was typing :P

Java KeyListeners/KeyBinding - Clear Buffer - Java Game Development

I'm working on a Java Game and I've come to a point where I'm having problems with the KeyListeners/KeyBinding. What I basically want to do is temporarily disable the keyboard/do not allow more inputs when an animation occurs. This animation is generated by updating data.
What I currently get is that I press the key for the animation, the animation starts, and I press another key that does some other function. It gets added to the stack/queue(?) of the keyboardlistener and triggers when the first animation finishes.
I'm using a JPanel that implements KeyListener.
To give an idea of the code:
public void keyPressed(KeyEvent arg0) {
//Prevents Repeated keys
pressed.add(arg0);
if (pressed.size() == 1) {
int key = ((KeyEvent) pressed.toArray()[0]).getKeyCode();
if (key == KeyEvent.VK_ENTER) {
doSomeAnimation();
} else if (key == KeyEvent.VK_SPACE) {
doADifferentAnimation();
}
Update();
}
}
Things I've tried:
1) Set the focusable(false) on the JPanel before the calls to the animations. Then set the focusable(true) and grab the focus when they've been completed.
2) Used a boolean to track when an animation occurred.
3) Use Key Bindings.
No matter what method I used, I always ended up with the problem that I would still take in input from the keyboard when the animation was occurring. Then, once that animation was finished, it'd go to the next element in the stack/queue(?) and process that. Also, these animations would need to occur more than once (so using an array of booleans to verify if it's been executed already wouldn't be helpful).
So, if you have any idea or help (or places to point me to) that would be greatly appreciated.
Some Extra Information: Java 1.6, IDE Eclipse, MVC structure. (This in question is the Controller/Model)
Assuming your animation is driven by an instance of javax.swing.Timer, the queue in question is the EventQueue, which runs events in "the same order as they are enqueued" by the Timer. Because it is impractical to stop other devices on the host platform from evoking your listeners, you have to track the effect in your application. As a concrete example, this game has several overloads of the model's move() method to handle input from disparate sources: keyboard, mouse or animation timer. The timer may be toggled on or off to see its effect. This example, which supplants the EventQueue with a custom implementation, may also offer some insight.

Categories