Trapping the arrow keys

Hi all,
Id like a programme that waited for user input and when the user pressed an arrow key it'd print out on the System.out.println saying so. For example if the user pressed the up arrow key (not followed by <enter> or anything), then the code would print
System.out.println("You pressed the up arrow key");
Thanks :-)

Thanks for the reply :-)
I've had a look at the API and have got reasonably far, the code compiles but it exits straight away, nothing is calling a keyListener for the keyPressed method will fire. This is the code so far.
package mypackage1;
import java.awt.event.*;
public class Class1 implements KeyListener
  public Class1()
  public void keyReleased(KeyEvent e) {  }
public void keyPressed(KeyEvent e)
   System.out.println(e.getKeyCode());  
   public void keyTyped(KeyEvent e) { }
  public static void main(String[] args)
  Class1 c = new Class1(); 
}

Similar Messages

  • How do I get the arrow keys to move one character at a time in the address bar or search box?

    I can no longer get the arrow keys to advance or retard "one character at a time" in the address bar, the search box, or on any form in Firefox. This feature works in other browsers without a problem. The "Home" and "End" keys do not work either. How do I fix this behavior?

    How do I configure the AX to connect to the hub's wireless network? It will only scan one base station Airport Express at a time in the Airport Utility. I think what I am trying to do is use the hub as a router to the two AX's. Is there a specific way to set this up?
    To set up the AX to join a wireless network as a "Wireless Client," using the AirPort Utility, either connect to the AX's wireless network or temporarily connect your computer directly (using an Ethernet cable) to the Ethernet port of the AX, and then, try these settings:
    Launch the AirPort Utility.
    Select the AX.
    If not already connected to the AX's wireless network, go ahead and switch to it when prompted.
    AirPort Utility > Select the AX > Manual Setup > AirPort > Wireless
    Wireless Mode: Join a wireless network
    Network Name: <type in the network name/SSID of the wireless network that you want to join or select it from the pop-up menu>
    Wireless Security: None OR, if you are using security on your wireless network:
    Wireless Security: <Select the appropriate level of security for the existing wireless network: WPA/WPA2 Personal | WPA2 Personal>
    Password: <enter your wireless network security password>
    Verify Password: <re-enter the same security password>
    Click Update to write the new settings to the AX.

  • 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,

  • 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

  • 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

  • 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

  • I have a Belkin bluetooth keyboard attached to my Ipad 3. I would like to know if there is any way to have the arrow keys scroll down when browsing web pages?

    I just got my Ipad 3rd generation a few days ago and for my case I got the bulky BELKIN Bluetooth keyboard. I notice that when I press down the arrow keys it does not seem to scroll down when searching a web browser or looking up web pages. However it works fine when scrolling down on office apps such as Pages. I'm wondering if is there a way to make the arrow keys on the keyboard scroll down when on an internet web page like my Mac does.

    Please read this whole message before doing anything.
    This procedure is a diagnostic test. It’s unlikely to solve your problem. Don’t be disappointed when you find that nothing has changed after you complete it.
    The purpose of the test is to determine whether the problem is caused by third-party software that loads automatically at startup or login, by a peripheral device, by a font conflict, or by corruption of the file system or of certain system caches.
    Disconnect all wired peripherals except those needed for the test, and remove all aftermarket expansion cards, if applicable. Start up in safe mode and log in to the account with the problem. You must hold down the shift key twice: once when you turn on the computer, and again when you log in.
    Note: If FileVault is enabled, or if a firmware password is set, or if the startup volume is a software RAID, you can’t do this. Ask for further instructions.
    Safe mode is much slower to start up and run than normal, with limited graphics performance, and some things won’t work at all, including sound output and Wi-Fi on certain models. The next normal startup may also be somewhat slow.
    The login screen appears even if you usually login automatically. You must know your login password in order to log in. If you’ve forgotten the password, you will need to reset it before you begin.
    Test while in safe mode. Same problem?
    After testing, restart as usual (not in safe mode) and verify that you still have the problem. Post the results of the test.

  • When I click on text in a page, an I-bar appears where I click which then can be moved with the arrow keys, just like a Word document.

    I have reinstalled Firefox twice, each time downloading a new installer exe from the website. I have also updated all plug-ins. This is a continual problem, as this makes normal scrolling impossible because the arrow keys move the I bar.

    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.

  • In latest Nightly, arrow keys don't move the cursor but instead exit the text box. How can I fix this so that the arrow keys move the cursor?

    Using Nightly 28.0a1, I'm having a problem with the text box fields when typing. I use Hacker's Keyboard which has onscreen arrow keys and I find it more convenient than fiddling with a tiny cursor thumb when editing text. On the Play Store Firefox it works just fine but on Nightly, any time I try to move the cursor with the arrow keys it jumps out of the text box and highlights other elements on the page. This is annoying beyond belief as it means having to resort to that imprecise thumb cursor that takes forever to get where you want it. Is this a known issue with the latest nightly builds?

    Thanks for the report. I have filed bug https://bugzilla.mozilla.org/show_bug.cgi?id=939959. It would be helpful to know and if you can comment into the bug to indicate which input field you are testing on.

  • 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).

  • Is there a way to adjust the Scroll bar? Because when I press the arrows key once it goes straight to the bottom of the page rather than going down gradually.

    Before when I pressed the arrow key to navigate up and down the screen it would do it gradually. After I up graded to the current version it now skips down to the very bottom of the page.

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

  • 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

  • When I highlight objects, I'm able to move them with the arrow key but I can't drag them with my mouse.

    When I highlight objects, I'm able to move them with the arrow key but I can't drag them with my mouse.

    Without system info and other details like e.g. your settings for bounding box display nobody can tell you much.
    Mylenium

  • 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.

Maybe you are looking for

  • How to set Current User in moss 2007 for Authentication....

    Hi Team, We have current running website where we have user id to login but now requirement is that user will login with his Email address. Email is stored in active directory, I'm able to login with fba authentication.   I'm able to retrieve the use

  • Preview arrows not showing in web search

    I have noticed today that when I am searching the web the preview arrows are missing so unable to preview a page quickly before deciding to open. Can anyone help, has anything changed with Safari?  (Normally use Google) Any help appreciated!

  • Pls help, flashing gone wrong :(

    Could anyone help me, I tried flashing my phone & something went wrong. Downloaded the french firmware & Flashtool To be precise, I connected my mobile through usb by pressing vol - , the green light appeared & it showed that my phone is connected in

  • Missing photos after Import

    Ok here is the deal, I have had problems iporting photos into my iphoto library lately. I imported a rill via the import command and then I got a munch of the ! mark things one for every picture. So then I went into the iphoto library and found that

  • How to Print the same field value in a single row

    Hi, I need to print the same field values in a single row For Examble in a table TestTable ID Name Value 1 AB 120 1 BC 150 1 CD 130 2 AB 111 2 BC 112 2 CD 113 I need the query like if the Name contains BC and CD then i need to print like ID, BC Value