Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default JFrames and KeyEvents

    Hey guys, so I go this code here:

    Java Code:
    JFrame wnd = new JFrame();
    // ...
    		wnd.addKeyListener(new KeyListener() {
    			@Override
    			public void keyTyped(KeyEvent ke) {
    				// Do nothing
    			}
    
    			@Override
    			public void keyPressed(KeyEvent ke) {
    				// Shifts the frames from right to left using the arrow keys
    				if (ke.isAltDown()) {
    					switch (ke.getKeyCode()) {
    						case KeyEvent.VK_COMMA:
    							globals.switchToFrame(globals.curFrame - 1);
    							globals.editAreaNeedsRepainting = true;
    							globals.frameInfoNeedsUpdating = true;
    							globals.timelineNeedsRepainting = true;
    							break;
    						case KeyEvent.VK_PERIOD:
    							globals.switchToFrame(globals.curFrame + 1);
    							globals.editAreaNeedsRepainting = true;
    							globals.frameInfoNeedsUpdating = true;
    							globals.timelineNeedsRepainting = true;
    							break;
    					}
    					System.out.println("CHECK!");
    				}
    			}
    
    			@Override
    			public void keyReleased(KeyEvent ke) {
    				// Do nothing
    			}
    		});
    It doesn't work at all, it doesn't even print out the "CHECK" like I want it to.

    But this works:
    Java Code:
    		kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    // ...
    		kfm.addKeyEventDispatcher(new KeyEventDispatcher() {
    			@Override
    			public boolean dispatchKeyEvent(KeyEvent ke) {
    				if (ke.isAltDown()) {
    					switch (ke.getKeyCode()) {
    						case KeyEvent.VK_COMMA:
    							globals.switchToFrame(globals.curFrame - 1);
    							globals.editAreaNeedsRepainting = true;
    							globals.frameInfoNeedsUpdating = true;
    							globals.timelineNeedsRepainting = true;
    							return true;
    						case KeyEvent.VK_PERIOD:
    							globals.switchToFrame(globals.curFrame + 1);
    							globals.editAreaNeedsRepainting = true;
    							globals.frameInfoNeedsUpdating = true;
    							globals.timelineNeedsRepainting = true;
    							return true;
    					}
    					
    					return false;
    				}
    				
    				return false;
    			}
    		});
    But instead of skipping ahead one frame, it goes ahead two frames.

    So how exactly do I get my code to work with "addKeyListener()"? I tried to do some research on the subject, and I read a few pages about a "Key Focus Manager," but that didn't work out quite well with me.

    And help is appreciated.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  2. #2
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Okay, problem solved, added:
    Java Code:
    wnd.setFocusable(true);
    To the code, makes it work properly.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

Similar Threads

  1. Question on Graphics/JFrame/KeyEvents
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 11:22 PM
  2. Replies: 3
    Last Post: 02-02-2011, 10:53 AM
  3. Automatic convert to uppercase in keyevents
    By ashin in forum SWT / JFace
    Replies: 4
    Last Post: 08-09-2010, 01:59 PM
  4. Question about java keyevents?
    By Godsent in forum AWT / Swing
    Replies: 2
    Last Post: 10-26-2009, 09:38 PM
  5. jframes
    By ddj in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •