Java animation, with key listener - java

I tried to make it so that,if I pressed the right/left key, the sprite/Mario would face right/left. If I pressed the right key, he would face right. But for some reason, When I pressed the left key, he won't face left.
Source Code: First ,Second
Images

"To fire keyboard events, a component must have the keyboard focus."—How to Write a Key Listener

Don't use a KeyListener. Instead use Key Bindings which are more flexible and are used by all Swing components.

Related

How to get the key pressed with an awt mouse event

I have a Swing application and I need to make a specific action if an alphanumeric key is pressed during a mouse drag action.
I successfuly managed the drag action but I can't get the key pressed.
It seems that only the modifiers (alt, ctrl, shift) are available in a mouse event?
Is there a a way to know if for example the W key is pressed in a java.awt.event.MouseAdapter method (without adding a keyListener) ?
Is there a a way to know if for example the W key is pressed in a java.awt.event.MouseAdapter method (without adding a keyListener) ?
No, you need to use a KeyListener.
And you also need to reverse your logic. That is when you handle the KeyEvent you need to check if the mouse is also being used by looking at the "modifiers" of the KeyEvent. Read the API for more information on the getModifiersEx() method.

Java swing: How do i detect if the key pressed will display output in my JTextArea?

I'm working on a text editor, and I want to be able to detect the difference between two distinct types of key events: the ones that result in a visible change to the text in my JTextArea (ie, alphanumeric characters, the enter key, symbols), and the ones that don't result in a visible change (directional keys, control keys, shortcuts). I know I can do this with a very verbose switch statement, but is there a less verbose way to do the following?
private void checkKey (java.awt.event.KeyEvent evt) {
if (saved && /*some way to check if the text in the box has changed */) {
editorTitle.setText(currentedit + " (Edited)");
saved = false;
}
}
Don't use a KeyListener. There are better API's to handle these situation.
If you want to know if the data in the text component has changed then you should be using a DocumentListener. It will generate an event whenever text is added or removed.
Read the section from the Swing tutorial on How to Write a DocumentListener for more information.
If you want to know when an arrow key is pressed to invoke some kind of Action then you should be using Key Bindings. This is how all Swing components work. A KeyStroke is bound to an Action.
See Key Bindings for more information and a complete list of the default key bindings of each component.
Shortcuts are also implemented in Swing by using Key Bindings.

How can I find the enter location for KEY_TYPED event?

I've got a KeyboardFocusManager with overridden dispatchKeyEvent() method to handle navigation for arrow key and enter. I need to handle key events for as long as the key is pressed. Unfortunately, holding down enter results in KEY_TYPED events which do not contain the key location. Is there a way to find out which enter is currently being pressed? Or can I suppress KEY_TYPED events for enter in favour of KEY_PRESSED events?
edit:
I can't tell you the reason why it's done with a KeyboardFocusManager instead of KeyListeners but I'm sure there were reasons it was done this way. As of now, it is not possible anymore to change this. The problem is, that for our system(an old terminal emulator), the left enter key is a navigational key. Pressing enter moves the focus to the next textfield after the current line. The right enter key is a command key that sends the user input to the server.
Using the KeyEventDemo found in How to Write a Key Listener, I am unable to reproduce the effect you describe for either KEY_PRESSED or KEY_RELEASED. Of course, "KEY_TYPED events do not have a keyLocation."
It's not clear why you are using a Focus Listener, as many components already bind certain keys to a defined Action. The example binds the arrow keys, which respond to auto-repeat events regulated by the host's preferences.

How to disable arrow keys for JSliders?

I want to use my arrows keys to move something on the screen but after I click on a JSlider to use it and I use the arrows keys, they are moving the slider instead of the object. I can’t find any method to disable this. (After clicking on a slider, the sliding arrow changes from a black outline to a blue one {my presumption indicating that the arrows keys are “locked” onto the slider.})
Key bindings should provide a solution to this problem.
Try slider.setFocusable(false);
But for a complex application, ensuring you always have control over what the arrows do may require the KeyboardFocusManager and KeyEventDispatcher APIs. These let you divert arrow (or any) keystrokes from normal handling, so must be used carefully.

How can I listen for keyboard input? such as arrow keys?

what is the easiest way to listen for key presses from the user? Specifically I am writing an image viewer program that uses a JFileChooser to select images and on the left side, a JList that shows the contents of the director. I would like to make the arrow kets (left/right) move to the next/previous file.
Thank you in advance.
It sounds like you need a KeyListener. The linked tutorial also talks about focus, which will be important to ensure your key events are trapped by the right handler.
Swing was designed to use Key Bindings.
In fact a JList already supports the up/down keys to move to the next item in the list. You can easily map these Actions to the left/right keys.

Categories