I'm building an application on Processing using NyARToolkit, but my question is not directly about NyArToolkit but about a key released() method.
The thing is, I show a card and then I can do a few things if I press different keys. I press "X" and it shows one thing, I press "Y" and it shows another thing. The problem is, it shows the info from the last key pressed all the time.
If I change my AR card, it will immediatly show the info from the last key pressed. I would like to do something to release the key, something like: just show while i'm pressing the key, or have an "ESC" to stop showing everything.
I've been reading about the keyreleased() method but I didn't figured it yet out to put it to work.
By the way my method is like this:
if(key == "c") then
else if(key =="d") then...
How about a boolean keeping track wether you've pushed something
private boolean buttonIsPressed = false;
and inside your keyPressed
buttonIsPressed = true;
and inside your keyReleased
buttonIsPressed = false;
This way you can keep track wether it's pressed or not, using a String you can also keep track which button is being held by initializing it in your keyPressed and making it null in your keyReleased.
If I missunderstood your question let me know.
EDIT:
Reading your edit, you're making quite a big mistake there, make sure to use .equals to compare strings so if(key.equals("c"))
Again, your question is not quite clear for me, so if I'm wrong, excuse me.
Related
So im creating a game of blackjack as part of an assignment for my college. I have to create some kind of method to allow the player to Hit or Fold.I decided to write this part of code which checks the input:
do {
System.out.println(playernames[j]+" (H/F)");
playerinput=input.next();
} while (!((playerinput.equals("H"))||(playerinput.equals("F"))));
Everything runs smoothly until it reaches this loop.The workspace im working(eclipse) doesnt show any syntax problems nor any java exceptions occur.I tried editing this to find what is happening by using a temp boolean set to false before the loop and after the loop it gets set to true.The output didnt change meaning it didnt get past the loop.
This code works, but only if the user types a capital H or F. Maybe that's the problem? If you want to allow both lower case and upper case letters, you can use playerinput.equalsIgnoreCase("H").
Also, input.next() will only return after the user has pressed enter. Just pressing the H or F key is not enough.
I have a simple Mario game and have key events that decide different animations and movements of the player.
They are:
Move Left
Move Right
Jump Left
Jump Right
As you can imagine, the player will only be able to play the jump right animation if he is moving right and is/has pressed the jump button
The issue I have come across is that only 1 key value may be sent at once. This makes if statements with && conditions useless.
Here was the code that did not work:
if(key == KeyEvent.VK_LEFT) {
if(distanceTraveled<300)dx=-5; else dx=-4;
if(key == KeyEvent.VK_UP)
player = jump_L_anim.getImage();
else
player = walk_L_anim.getImage();
} else {
if(distanceTraveled<300)dx=5; else dx=4;
if(key == KeyEvent.VK_UP)
player = jump_R_anim.getImage();
else
player = walk_R_anim.getImage();
}
Simple enough, I realized that there are solutions to this and began attempting to figure out the key pressed before the current one.(this key could not be equal to the one currently pressed).
This would allow an if statement such as
if key==right && lastKey == up
run jump_R animation
I managed to be able to grab the last key, but not the last key that was not a repeat.
I felt really confused and decided to watch this missile guidance for dummies video. That made me even more confused, In my boring accounting class I figured out the solution must be to store all previous values in an array, then I would loop through the values from the last to the beginning. I would continue this until the numbers would not continue. The number where the repetition stops is the last value i am looking for.
For anyone confused by this take a look at these console readings of key movements
48
48
48
49
49
49
49
47
47
47
The number I am looking for in this situation is 49. That is because it was the last key pressed that is not the current key.
Is my logic behind this issue correct? How would you solve this issue? I prefer not working with arrays if there is a simple variable only solution available. I am looking for the fastest and most efficient logic.
Perhaps you can store which keys are held down at the moment into an HashSet, and the if statement would be something like:
if (keyCodeSet.contains(x) && keyCodeSet.contains(y))
where x and y are the key numbers you want. When the keys are released, you simply remove them from the set.
You should keep track of when the keys are released. If up is never released, you know it's still being held. If right is never released, you know it's still being held. Each update, you can check if both are down and react accordingly.
It's actually totally simple:
Listen to both keyPressed and keyReleased
use a Set<Key>1 or just 4 variable for the keys you're interested in
when a key gets pressed set, add it to the set
when a key gets released, remove it
On any key press, check if the other key is there. This way you can distinguish "Up+Right" from "Right+Up" if you want to.
1 Key may be Integer or whatever representation you prefer.
If I held down the keys up and down, and pressed a button that calls Display.update and ran the following code:
while (!Keyboard.isKeyDown(Keyboard.KEY_SPACE);
Display.update();
boolean up = Keyboard.isKeyDown(Keyboard.KEY_UP);
Thread.sleep(500);
boolean down = Keyboard.isKeyDown(Keyboard.KEY_DOWN);
if (up && down)
System.out.println("Both keys detected");
What would be the outcome? I'm not sure if I should check for all the keys at once during the game update or just when I need them.
I don't understand your question at all but ill try to answer. I have no idea how or where you update your keys, but if you update your key listener, then your program will check if the booleans are true or false. I think you need to learn some basic java before going on to OpenGL because this is a question someone with less than a couple weeks of coding experience would ask. Also, its relative not relitive.
i think it is not relative to Display.update as it is not logical to hold all the booleans for the keys and update the every half a millisecond.
there for i say it is independant of Display.Update
I mostly fixed the problem with these lines in dispatchKeyEvent:
byte[] cmdLeft = { (byte) 27, (byte) '[', (byte) 'D' };
byte[] cmdErase = { (byte) 27, (byte) '[', (byte) 'P' };
mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
mSession.appendToEmulator(cmdErase, 0, cmdErase.length);
The only problem now is that if I select the editText and hit delete then one character is deleted but two appear to be on screen. so if I write enable and hit delete it will change to enab but what would actually be sent is enabl
I overrode dispatchKeyEvent, and it kind of works. If the editText is selected, the terminal deletes characters over serial now, so that is a good step. However the main problem still exists that if the terminal is selected itself, weird little boxes are written to the screen instead of deleting a character. Well one is written, and if I keep pressing delete it stays at that one box, but next time I type the amount of deletes I pressed comes up as boxes. It's very odd...
It's like it is just overridden for the edittext and not for the terminal.
Weird little boxes in all their glory:
public boolean dispatchKeyEvent(KeyEvent event) {
if (event != null && event.getAction() == KeyEvent.ACTION_UP) {
return false;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
try {
sendOverSerial("\b".getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return super.dispatchKeyEvent(event);
};
I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data. I send data over the connection via a text box below the terminal or by typing in the terminal itself and hitting enter on the keyboard in both cases. It will only ever be a soft keyboard that is used. If I send an incorrect string I am in an unrecoverable state due to not having a delete key implementation. Backspace in my editTxt works fine, I just want it to work when the terminal is highlighted and I am writing in that.
At the moment if I press delete a little odd box character comes up and nothing else happens, I get an exception in the log some times(http://i.imgur.com/wMRaLPX.png). What I want to know is how to I go about changing the delete keys functionality so that when I press it I can send a delete character like this but also retain the ability to delete characters in the edittext box etc:
sendOverSerial("\b".getBytes("UTF-8"))
This sends a correct back space, I just need to incorporate it.
But the soft-keyboard doesn't seem to register key presses? i keep getting a keycode of 0 and only enter will work.
I am currently trying out https://stackoverflow.com/questions/4...62035_11377462, but any other suggestions would be great, as about 10 suggestions haven't worked so far. My backspace wouldn't be associated with an editText, but a terminal View. I can't even detect the delete key being pressed.
It looks like the terminal control you are using must be consuming the KEYCODE_DEL instead of letting it propagate to the window and it must be sending a different char to the remote end instead of \b. So when your edit text is focused your dispatchKeyEvent is handling the press - but you don't see it when the terminal has focus. Have you confirmed that the even handler is firing via debugger when the terminal has focus? You didn't say which library you are using for the terminal, but I'd look at that and see if you can set a key handler or something.
I don't have any experience with Android, and I'll also admit I've never tried to implement a delete/backspace key bind. However, if I were trying to do this, and I didn't know a good standard implementation I can think of a workaround that would probably function just fine. Make a key bind to delete with an associated action listener. Make the action listener getText() out of your text field and store it as a String. Substring that string to include everything but the last character. Then use setText() for the text field with the new string. Kind of a manual way of doing it, but it would definitely work.
I recommend capturing the full string and send it all at once, when the user presses Send, like a chat program.
The solution was to move the method that wrote to the screen to another class, then everything worked fine.
I cannot get the android "delete" key to register in my TextField (scene2d ui element in libgdx) listener. Here is my code to define the text field:
nameTextfield = new TextField("", skin);
nameTextfield.setMessageText("Some Text");
uiStage.addActor(nameTextfield);
I tried this listener just to decode the keycode for the DELETE key:
nameTextfield.setTextFieldListener(new TextFieldListener() {
public void keyTyped (TextField textField, char key) {
textField.setText(String.valueOf(Integer.valueOf(key)));
}
});
Although it gives code for almost for all buttons, it doesn't even react on DELETE button.
I tested this on a Nexus 7.
From the TextField.java source it looks like the "DELETE" (and "BACKSPACE", and "TAB" and a couple other keys ) are handled specially by the TextField. These keys are never forwarded to any listener.
The built-in handler should do "the right thing" (trimming characters off the string contents).
Is delete not behaving correctly for your case in some way that led you to try to decode it?
Well the DELETE button should be implemented differently.
I suggest trying to verify if the pressed key is the DELETE button. If it is, you just do textField.getText(), trim the last letter off it, and set the new text with setText.
I'm sure there's a much more elegant way to do this, but it's the only workaround I can think of. After all, DELETE isn't really a char which you can throw inside setText. Is it? :/
LATER EDIT:
Print the key variable inside your listener, put a breakpoint there, and see what value is assigned to it.
Then also print (or check the javadoc) KeyEvent.KEYCODE_DEL (documentation here) to see what value this one takes.
the best way to solve this is the following:
You will need to listen to inputs from another listner, for example the same screen.
MainMenuScreen implements Screen, InputProcessor
then you will need to create a multiplexer to let inputs been listen from both the stage and the listner.
multiplexer = new InputMultiplexer();
then add the two listners:
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Now you will have to simply delete the field from here:
#Override
public boolean keyDown(int keycode) {
Gdx.app.log("Debug:", "keydown : "+keycode);
//DO SMOTHING LIKE
// if(keycode==...) deleteTextField();
return true;
}
Let me know if you have questions about this solution.
Worked great for me.
This issue has been solved by the latest nightly version of libgdx, the issue is known and discussed in the following link:
nexus button