Key Bindings using the arrow keys

Following is some code that allows you to move a component around the screen using the arrow keys. I've noticed a problem and I'm just wondering if its Java or my keyboard.
Lets start with an example that works:
Press the down and right keys. Then press either the up or left key. The image moves proving that it supports 3 keys.
Now for the problem:
Press the up and left keys. Then press either the down or right key. Three things to notice:
a) the direction doesn't change when the third key is pressed
b) no output is displayed, so the ActionListener is not being invoked
c) after a short time, the program starts beeping
Now try rerunning the code after removing the comments that assign the key bindings to the "a, s, d, f" keys. Redo the above test and it works even if all four keys are pressed.
I don't remember this problem when I wrote the code a while ago, but I did recently get a cheap new keyboard so I'm just wondering if the problem is my keyboard or whether its a quirk with Java.
You can download Duke from here:
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/LayeredPaneDemoProject/src/components/images/dukeWaveRed.gif
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyboardNavigationProblem implements ActionListener
     private JComponent component;
     private int deltaX;
     private int deltaY;
     private Timer timer;
     private int keysPressed;
     private InputMap inputMap;
     public KeyboardNavigationProblem(JComponent component, int delay)
          this.component = component;
          this.deltaX = deltaX;
          this.deltaY = deltaY;
          timer = new Timer(delay, this);
          timer.setInitialDelay( 0 );
          inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
     public void addAction(int keyCode, String description, int deltaX, int deltaY)
          new NavigationAction(keyCode, description, deltaX, deltaY);
     public void updateDeltaX(int delta)
          deltaX += delta;
     public void updateDeltaY(int delta)
          deltaY += delta;
     public void actionPerformed(ActionEvent e)
          int componentWidth = component.getSize().width;
          int componentHeight = component.getSize().height;
          Dimension parentSize = component.getParent().getSize();
          int parentWidth  = parentSize.width;
          int parentHeight = parentSize.height;
          //  Determine next X position
          int nextX = Math.max(component.getLocation().x + deltaX, 0);
          if ( nextX + componentWidth > parentWidth)
               nextX = parentWidth - componentWidth;
          //  Determine next Y position
          int nextY = Math.max(component.getLocation().y + deltaY, 0);
          if ( nextY + componentHeight > parentHeight)
               nextY = parentHeight - componentHeight;
          //  Move the component
          component.setLocation(nextX, nextY);
     class NavigationAction extends AbstractAction implements ActionListener
          private int deltaX;
          private int deltaY;
          private KeyStroke pressedKeyStroke;
          private boolean listeningForKeyPressed;
          public NavigationAction(int keyCode, String description, int deltaX, int deltaY)
               super(description);
               this.deltaX = deltaX;
               this.deltaY = deltaY;
               pressedKeyStroke = KeyStroke.getKeyStroke(keyCode, 0, false);
               KeyStroke releasedKeyStroke = KeyStroke.getKeyStroke(keyCode, 0, true);
               inputMap.put(pressedKeyStroke, getValue(Action.NAME));
               inputMap.put(releasedKeyStroke, getValue(Action.NAME));
               component.getActionMap().put(getValue(Action.NAME), this);
               listeningForKeyPressed = true;
          public void actionPerformed(ActionEvent e)
               if (listeningForKeyPressed)
                    updateDeltaX( deltaX );
                    updateDeltaY( deltaY );
                    inputMap.remove(pressedKeyStroke);
                    listeningForKeyPressed = false;
                       if (keysPressed == 0)
                            timer.start();
                 keysPressed++;
               else // listening for key released
                    updateDeltaX( -deltaX );
                    updateDeltaY( -deltaY );
                    inputMap.put(pressedKeyStroke, getValue(Action.NAME));
                    listeningForKeyPressed = true;
                    keysPressed--;
                       if (keysPressed == 0)
                            timer.stop();
               System.out.println(KeyboardNavigationProblem.this.deltaX + " : "
               + KeyboardNavigationProblem.this.deltaY);
     public static void main(String[] args)
          JPanel contentPane = new JPanel();
          contentPane.setLayout( null );
          JLabel duke = new JLabel( new ImageIcon("dukewavered.gif") );
          duke.setSize( duke.getPreferredSize() );
          duke.setLocation(100, 100);
          contentPane.add( duke );
          KeyboardNavigationProblem navigation = new KeyboardNavigationProblem(duke, 100);
          navigation.addAction(KeyEvent.VK_LEFT, "zLeft", -5, 0);
          navigation.addAction(KeyEvent.VK_RIGHT, "zRight", 5, 0);
          navigation.addAction(KeyEvent.VK_UP, "zUp", 0, -5);
          navigation.addAction(KeyEvent.VK_DOWN, "zDown", 0, 5);
//          navigation.addAction(KeyEvent.VK_A, "zLeft", -5, 0);
//          navigation.addAction(KeyEvent.VK_S, "zRight", 5, 0);
//          navigation.addAction(KeyEvent.VK_D, "zUp", 0, -5);
//          navigation.addAction(KeyEvent.VK_F, "zDown", 0, 5);
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          frame.setContentPane( contentPane);
          frame.setSize(800, 600);
          frame.setLocationRelativeTo( null );
          frame.setVisible(true);
}

if you hold down left and right (so it doesn't move), down works, but not up.
hold up/down, right works but not left.Yes, the problem only seems to be when the up and left in combination with the right or down key is pressed.
num lock off - use the number pad arrow keys and all works OK Thats interesting, I didn't notice that. Although it just confuses the issue as to what the problem is.
So it appears to me that:
a) left + up + down, and
b) left + up + right
are special key combinations that are intercepted by either the OS or the JVM. Do these key combinations ring a bell to anybody?
I'm use JDK1.4.2 on XP. I wonder if other OS will have the same problem?
Again, this isn't a real, problem, just more of a curiosity. Thanks,

Similar Messages

  • Firefox is placing a cursor in the html window of pages that cannot be edited messing up the scroll option using the arrow keys

    Whenever I click in a window pane to scroll using my arrow keys it is placing a cursor in the window and scrolling off of it, instead of scrolling the entire page by using the arrow key it is scrolling by the cursor location inside of the window like a word document.

    Hit '''F7''' to turn off caret browsing.
    http://kb.mozillazine.org/Accessibility.browsewithcaret

  • I am having a problem moving across cells in Microsoft Excel. When I use the arrow keys, the sheet moves, instead of moving to the next cell. Would anyone have any thoughts on this, please?

    I am having a problem moving across cells in Microsoft Excel. When I use the arrow keys, the sheet moves, instead of moving to the next cell. Would anyone have any thoughts on this, please?

    Hi,
    Did you find a way to solve your problem ? We have exactly the same for one application and we are using the same version of HFM : 11.1.2.1.
    This problem is only about webforms. We can correctly input data through Data Grids or Client 32.
    Thank you in advance for your answer.

  • When adding bookmarks and trying to changing its name, Firefox won't allow using the mouse; Firefox will only allow using the arrow keys (MAC OS 10.5, Firefox 3.6.8)

    When adding a bookmark sometimes the web page name is too long. I used to be able to use my mouse, highlight the letters/words that I don't want, then use delete to truncate the desired bookmark name. Since the latest update/upgrade, Firefox won't let me use the mouse to click anywhere in the popup box. I can only use the arrow keys and the delete key.
    Nothing other than updates (using Mac Software Update and Firefox Update) has been done to this computer (it's a 2002 Power Mac G4-Dual 1.25GHz).

    When adding a bookmark sometimes the web page name is too long. I used to be able to use my mouse, highlight the letters/words that I don't want, then use delete to truncate the desired bookmark name. Since the latest update/upgrade, Firefox won't let me use the mouse to click anywhere in the popup box. I can only use the arrow keys and the delete key.
    Nothing other than updates (using Mac Software Update and Firefox Update) has been done to this computer (it's a 2002 Power Mac G4-Dual 1.25GHz).

  • When i try and open a tab that is the same as the page im currently on i closes it self. not when i type the full address, but when i use the arrow keys to select the url and press enter

    when I try and open a tab that is the same as the page I'm currently on it closes it self. Not when I type the full address, but when I use the arrow keys to select the url and press enter. I just don't like typing in the same address 5 times, when the older Firefox worked.

    Hi
    AutoPunch enabled? Command click in the bottom half of the Bar Ruler to turn it off
    CCT

  • Using the arrow keys to move objects in photoshop issue

    When I move objects around in Photoshop using the arrow keys something odd to me happens.  When I press the left arrow key it seems to move 2 pixels then when I press the right arrow key it seems like it moves 1 pixel.  I noticed this when I had 3 objects.  One was centered and I wanted to move both the other objects 20 pixels away.  One looked twice as far away.  I looked closer and I found out that was the problem.  This also happens when using the up and down arrow keys.  Is that supposed to happen and is there a way to fix it? Thanks

    BOILERPLATE TEXT:
    If you give complete and detailed information about your setup and the issue at hand, such as your platform (Mac or Win), exact versions of your OS, of Photoshop and of Bridge, machine specs, such as total installed RAM, scratch file HDs, video card specs, what troubleshooting steps you have taken so far, what error message(s) you receive, if having issues opening raw files also the exact camera make and model that generated them, etc., someone may be able to help you.
    Please read this FAQ for advice on how to ask your questions correctly for quicker and better answers:
    http://forums.adobe.com/thread/419981?tstart=0
    Thanks!

  • When I use the arrow keys in firefox to go forward and back, the cursor erases tiny bits of the letters.

    Even within this text field as I type, all is normal, but backspacing using the arrow keys (and then also forward spacing using the arrow keys as I return to typing) erases bits of the text. Once I begin typing again in this field, the erased components restore to normal. This effect can be captured in a screenshot. Upon closer inspection, there appears to be a second invisible cursor, one full character to the right of the visible cursor. So when arrow backspacing, the invisible cursor trails the visible one, wrecking the text behind. When forward spacing using the arrow keys the text gets wrecked in front of the visible cursor.

    I am guilty of not following up. The above solution "appeared" to work for me until I realized that now, the text does not erase bits on the same line as my cursor but erases any text on the line below my cursor... so if I am typing above other text, these semi-invisible marks chop up the text below where I am backspacing. Yes, I know - put this in David Letterman's, "Museum of the hard to believe".

  • How do I change the speed of the cursor when using the arrow keys?

    How do I change the speed of the cursor when using the arrow keys?

    Applications folder or Apple icon > System preferences > Keyboard increase key repeat.

  • I can't scroll using the arrow keys!!

    I have been having a problem lately where I cannot scroll within applications using the up and down arrow keys. on my macbook, using the arrow keys is something i liked to do to scroll in a page instead of using the trackpad, and now it doesn't work... I've noticed it in every web browser installed on the laptop (Safari, Firefox, Camino) and also doesn't work in itunes (making it annoying to easily scroll to another song in the library!) or AIM or other programs... anybody know how I can fix this? thanks.

    Hello and Welcome to Apple Discussions!
    Full keyboard access is useful for people who have difficulty using a mouse. Safari supports standard full keyboard access shortcuts for buttons and menus. If you choose, you can also use the keyboard to highlight links in webpages.
    To turn on full keyboard access:
    Choose Apple menu > System Preferences and click Keyboard & Mouse.
    Click Keyboard Shortcuts.
    Select the checkbox to turn on full keyboard access.
    To highlight links in webpages using the Tab key:
    Choose Safari > Preferences and click Advanced.
    Select the option to highlight each item on the web page using the Tab key.
    With full keyboard access turned on, Safari uses the standard shortcut keys to access menus and windows, plus the additional shortcuts shown in the table.
    Hope this helps!!
    Carolyn

  • How can I control the order of iDVD menu items using the Arrow keys?

    I have a submenu screen containing 10 selectable buttons plus the Return Arrow button.  I have ordered all the slideshows in the correct order in the DVD map.  However, when I run the program, the arrow keys jump the selection focus in an apparently random sequence.  How can I control the order of the selection focus?
    I'm using iDVD v 7.1.2
    TIA.
    Ron

    Hi
    There is one thing You might miss in iDVD - So do I - the ability to re-arrange in the DVD map (block diagram)
    The order things will be pplayed or addrssed is same as the order each item is introduced into iDVD.
    To my knowledge ther is no way around this.
    Yours Bengt W

  • My up and down arrows as well as page up and page down no longer work properly only when using firefox. Page up and down function more like the "home" and "end" keys as do the arrow keys.

    Honestly I'm not sure if these began just after I updated FF to the current release, but it's been happening for at least 2 weeks. The arrow keys and page up and page down keys all work as if I'm pressing the 'home' and 'end' keys on my keyboard. This is only when using FF. Please contact me if you need more clarification. Thanks in advance!

    Sounds that you have switched on caret browsing.
    * http://kb.mozillazine.org/accessibility.browsewithcaret
    You can press press F7 (on Mac: fn + F7) to toggle caret browsing on/off.
    * Tools > Options > Advanced : General: Accessibility: [ ] "Always use the cursor keys to navigate within pages"
    * http://kb.mozillazine.org/Scrolling_with_arrow_keys_no_longer_works
    * http://kb.mozillazine.org/Accessibility_features_of_Firefox

  • The scroll bar on the right is gone. the only way i can move the screen up or down is by using the arrow keys.  happened after i updated software is there something i missing

    The scroll bar on the right is gone  the only way I can move the screen up or down is with the arrow keys  Why how do i get it  back and have it stay there

    Yep. For all us spreadsheet geeks, this is a real sinker. Anyway, here's my workaround. Not free, unfortunately. I have a macro program called QuicKeys installed on my Mac that I've used for countless years, and  which I highly recommend for other reasons. Anyway, one of QuicKeys' many features is simulating a scroll wheel using the keyboard. I have it simulating a cell-by-cell scroll using cmd-arrow keys. The only drawback is that I have to release the cmd key each time I want to move another cell.

  • I can only scroll using the arrow keys. In IE/C I can use the touch pad to slide my fingers to scroll. Why cant i do this in FF?!?!

    On my laptop (and most for that matter) you can use the touch pad to slide your fingers to scroll thru a web page. I cannot do this in firefox. It is very annoying and I dont know why cant do this?
    I have reinstalled FF and it still wont scroll! Every other browser i can scroll thru a web page using the touch pad method.
    Why cant i do this on FF?

    Hello and Welcome to Apple Discussions!
    Full keyboard access is useful for people who have difficulty using a mouse. Safari supports standard full keyboard access shortcuts for buttons and menus. If you choose, you can also use the keyboard to highlight links in webpages.
    To turn on full keyboard access:
    Choose Apple menu > System Preferences and click Keyboard & Mouse.
    Click Keyboard Shortcuts.
    Select the checkbox to turn on full keyboard access.
    To highlight links in webpages using the Tab key:
    Choose Safari > Preferences and click Advanced.
    Select the option to highlight each item on the web page using the Tab key.
    With full keyboard access turned on, Safari uses the standard shortcut keys to access menus and windows, plus the additional shortcuts shown in the table.
    Hope this helps!!
    Carolyn

  • When I use the arrow keys to decrease/increase the amount of segments in line art it alters too much, it goes to either hundreds or almost deletes shape, how do I fix?

    HI,
    I'm new to adobe and would really appreciate some help! I'm trying to follow a Lynda tutorial and we are using the line art tool, I was doing fine until he's mentioned you can use the arrows to increase/decrease the number of segments in the spiral, bulls eye etc. He seems to be able to go up and down gradually where as I jump from, say, 8 to zero or down to two or none. Any help is appreciated, Thanks.

    lindenblossoms,
    You may have a look at Edit>Preferences>General>Keyboard Increment, at least to start with.

  • Why can't I use the arrow keys to scroll up and down websites anymore?

    When I'm on any website and I click anywhere in the website there is a blinking bar - sorry, don't know the technical term - like you would find in any new document waiting for the next character to appear to the left of it. It's almost as if FF - not the super-team - want me to add test to any page I find but it is just a weird mistake.
    I assume it's because of this glitch (assuming) that when I go to press the up or down arrow keys it no longer cascades the screen smoothly like a parade of content; instead it just hops from top to middle to bottom and that's when it does anything. What gives?

    -> Tap ALT key or press F10 to show the Menu Bar
    -> go to Tools Menu -> Options -> Advanced -> General section -> place Checkmark on '''Use Smooth Scrolling''' -> Remove Checkmark from '''Use AutoScrolling''' -> click OK
    Check and tell if its working.

Maybe you are looking for

  • View creation very slow - not materialized view

    I have a view creation script which is running in a fraction of a second in the dev environment but takes over an hour to run in the test environment. The test environment is off-site, so I have no direct access to it. It does have a lot more data in

  • "Disk cannot be read from or written to" When syncing CERTAIN songs to iPod

    So I bought my friend's iPod Touch. I've talked to her about this issue and she's never experienced anything of the sort. When I got her iPod Touch I downloaded the newest software (3.1.2). It's still currently running on 3.1.2 (as I know of no way t

  • Unresponsive keyboard Windows 7 Boot camp install

    Im trying to install Windows 7 onto my mid 2010 MacBook and seem to hit every problem possible. Now after I partitioned the hard drive and clicked continue, the "Windows is reading files" comes up and eventually an error occurs. "Windows has encounte

  • .pdf and .doc Attachments are downloading as .html files

    Dear Forum, I have received both .doc and .pdf attachments in my email (charter.net). I click to download, the files then appear on the desktop as Safari files with a example.doc.html file name or example.pdf.html Of course neither Word or Acrobat Re

  • In mini bridge is there a key stroke to "select all" images?

    Hello, In mini bridge is there a key stroke to "select all" images? I'd prefer a key stroke instead of having to use the View drop down menu to select all, and "Ctl A" doesn't do anything. Thank you.