How to use KeyListener

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html Check this tutorial If it’s a UI based application , then ” I also need to know what I need to add to my code so that my program waits about 700 milliseconds for a keyinput before moving on to another method” you can use GlassPane or Timer class to fulfill the requirement. For … Read more

Why can’t I access my panel’s getWidth() and getHeight() functions?

Swing components have no width or height until they’ve been rendered. This occurs if you call pack() or setVisible(true) on a root container. Consider placing your x y int initialization code in the componentResized method of a ComponentListener that is added to your JPanel. e.g., import java.awt.event.*; import java.awt.*; import javax.swing.*; public class TestLinePanel { … Read more

Keylistener in Javascript

Here’s an update for modern browsers in 2019 let playerSpriteX = 0; document.addEventListener(‘keyup’, (e) => { if (e.code === “ArrowUp”) playerSpriteX += 10 else if (e.code === “ArrowDown”) playerSpriteX -= 10 document.getElementById(‘test’).innerHTML = ‘playerSpriteX = ‘ + playerSpriteX; }); Click on this window to focus it, and hit keys up and down <br><br><br> <div id=”test”>playerSpriteX … Read more

Jtextfield and keylistener

Don’t use KeyListener on text components, there are a rafter of issues (not been notified, mutation exceptions, not been notified when the user pastes something into the field), instead, you should be using a DocumentFilter For example… import java.awt.EventQueue; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; … Read more

KeyListener vs. Key Bindings

KeyListener is a much lower level API which requires the component that it is registered to be focused AND have keyboard focus. This can cause issues when you have other components within your game that may grab keyboard focus, for example. KeyListener is generally more difficult to maintain and extend or change, as typically, all … Read more

Allowing the “Enter” key to press the submit button, as opposed to only using MouseClick

There is a simple trick for this. After you constructed the frame with all it buttons do this: frame.getRootPane().setDefaultButton(submitButton); For each frame, you can set a default button that will automatically listen to the Enter key (and maybe some other event’s I’m not aware of). When you hit enter in that frame, the ActionListeners their … Read more

addKeyListener() doesn’t work for JPanel

Simply, your panel needs to be focusable. Add in wherever you create the panel: panel.setFocusable(true); panel.requestFocusInWindow(); Here’s a SSCCE (I suggest asking questions with one of these in the future): import java.awt.Dimension; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class SimpleKeyTest { public static void main(String[] args) { Runnable r = … Read more