key-bindings

java keylistener not called

[亡魂溺海] 提交于 2019-11-26 06:45:12
问题 I have written a sample code using KeyListener in Java, I have created a JPanel , then set its focusable to true, I have created a KeyListener, requested a focus and then added the KeyListener to my panel. But the methods for the keyListener are never called. It seems although I have requested focus, it does not focus. Can anyone help? listener = new KeyLis(); this.setFocusable(true); this.requestFocus(); this.addKeyListener(listener); class KeyLis implements KeyListener{ @Override public

KeyListener, keyPressed versus keyTyped

杀马特。学长 韩版系。学妹 提交于 2019-11-26 04:25:28
问题 I have a JFrame (well, a class which extends JFrame) and I want to do an action when I press the F5 key. So, I made the class implement KeyListener. And with that, came three methods, keyPressed, keyReleased, and keyTyped. Which of these methods should I use to listen for F5 being pressed? keyPressed or keyTyped? I currently have the following, however it does not print anything when I press F5. public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_F5) System.out.println(\"F5

Key bindings vs. key listeners in Java

梦想的初衷 提交于 2019-11-26 02:16:10
I note that in Java / Swing there seem to be at least two different ways of handling key events: Key Bindings Key Listeners What are the advantages / disadvantages of each, and when should you prefer one rather than the other? when should you prefer one rather than the other? Prefer Key Bindings since they were introduced. A KeyListener is a lower level connection with events. That page for the key bindings covers a lot of the reasons I would tend to use them rather than a KeyListener . It lists many things which are simply 'not available' to a KeyListener . E.G. choices of: WHEN_FOCUSED WHEN

Java KeyListener Not Registering Arrow Keys

一笑奈何 提交于 2019-11-26 02:01:44
问题 I\'m writing a simple program in Java which includes a KeyListener with the following overriding they KeyTyped method: @Override public void keyTyped(KeyEvent e) { int key = e.getKeyCode(); System.out.println(\"TEST\"); if (key == KeyEvent.VK_KP_LEFT || key == KeyEvent.VK_LEFT) { System.out.println(\"LEFT\"); //Call some function } else if (key == KeyEvent.VK_KP_RIGHT || key == KeyEvent.VK_RIGHT) { System.out.println(\"RIGHT\"); //Call some function } } When I type anything other than the

Key bindings vs. key listeners in Java

戏子无情 提交于 2019-11-26 01:09:46
问题 I note that in Java / Swing there seem to be at least two different ways of handling key events: Key Bindings Key Listeners What are the advantages / disadvantages of each, and when should you prefer one rather than the other? 回答1: when should you prefer one rather than the other? Prefer Key Bindings since they were introduced. A KeyListener is a lower level connection with events. That page for the key bindings covers a lot of the reasons I would tend to use them rather than a KeyListener .

Threads with Key Bindings

こ雲淡風輕ζ 提交于 2019-11-25 23:44:57
问题 I\'m new to Java graphics and threads, and I\'m trying to make a game (specifically, Pong). The idea is that two people can play on the same keyboard (i.e. there are two paddles that are controlled through different keys). Currently, both players can\'t move their paddle at the same time. Is there a solution to this? Are separate threads the answer? If possible, I\'d like the paddles to be able to move (at least seemingly) at the same time. Update: It seems like using a Set<Integer> to store

Binding arrow keys in JS/jQuery

非 Y 不嫁゛ 提交于 2019-11-25 23:29:12
问题 How do I go about binding a function to left and right arrow keys in Javascript and/or jQuery? I looked at the js-hotkey plugin for jQuery (wraps the built-in bind function to add an argument to recognize specific keys), but it doesn\'t seem to support arrow keys. 回答1: $(document).keydown(function(e) { switch(e.which) { case 37: // left break; case 38: // up break; case 39: // right break; case 40: // down break; default: return; // exit this handler for other keys } e.preventDefault(); //

How to use Key Bindings instead of Key Listeners

半腔热情 提交于 2019-11-25 22:33:46
问题 I\'m using KeyListeners in my code (game or otherwise) as the way for my on-screen objects to react to user key input. Here is my code: public class MyGame extends JFrame { static int up = KeyEvent.VK_UP; static int right = KeyEvent.VK_RIGHT; static int down = KeyEvent.VK_DOWN; static int left = KeyEvent.VK_LEFT; static int fire = KeyEvent.VK_Q; public MyGame() { // Do all the layout management and what not... JLabel obj1 = new JLabel(); JLabel obj2 = new JLabel(); obj1.addKeyListener(new