JSlider -- catch when arrow keys are pressed

Hi,
With a JSlider, when it has focus and a user presses the left/right arrow keys ..the thumb on the slider is moved to a new value. I tried adding a keyListener but that didnt work...how can I catch when a left/right arrow key is pressed??
thanks

You can take over the binding, but you will be responcible for the action....
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class KeyMappingEx extends JFrame
JSlider slid;
    public KeyMappingEx()
        super( "Key Mapping Ex");
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        slid  = new JSlider( 0, 100, 50 );
          slid.setMajorTickSpacing( 20 );
        slid.getInputMap( ).put( KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false),
                                   "RIGHT_ARROW" );
        slid.getActionMap().put( "RIGHT_ARROW", new RightAction(  ) );
        slid.getInputMap( ).put( KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false),
                                   "LEFT_ARROW" );
        slid.getActionMap().put( "LEFT_ARROW", new LeftAction(  ) );
        setContentPane( slid );
        pack();
        setLocationRelativeTo( null );
        setVisible( true );
    public static void main( String[] args )
        try
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        catch( Exception e )
        new KeyMappingEx();
    class RightAction extends  AbstractAction
        public void actionPerformed(ActionEvent e)
            System.out.println("RIGHT ARROW");
            slid.setValue( slid.getValue() + slid.getMajorTickSpacing() );
    class LeftAction extends  AbstractAction
        public void actionPerformed(ActionEvent e)
            System.out.println("LEFT ARROW");
            slid.setValue( slid.getValue() - slid.getMajorTickSpacing() );
}

Similar Messages

  • No key event when compose keys are pressed

    I am supporting a legacy application for Sabre which maps some of the keys differently. For example, on a French keyboard the Compose key which is used to put a ^ on top of the vowel characters is to be remapped to produce a special character called a "Change Character" that has a special use for reservation agents that use Sabre
    The problem is, the first time you press the ^ compose key there is no key event apparently because the VM figures it doesn't yet know what key to report, it is expecting you to press a character such as 'o' and then you get an 'o' with a '^' on top of it.
    If you press the ^ compose key a second time, you get the following key events:
    keyPressed
    keyTyped
    keyTyped
    keyReleased
    On the screen you will see two ^ characters appear. Currently I remap the ^ character to the "Change Character", and I suppress the second keyTyped event so that I only get one character. The problem is that the user ends up having to press the key twice to get one "Change Character."
    I have no fix for this problem because there is no key event produced when they press the ^ compose key the first time.
    By the way, this behavior appears to have been introduced with jdk 1.3. The older jdk did produce a key event the first time you pressed the compose key. I would expect that this behavior was considered to be a bug and was fixed in jdk 1.3.
    Is there some other way to detect when the user presses a compose key? If not, is it possible for future jdk releases to report a keyPressed event when a compose key is pressed? This event would not cause a character to appear on the screen, but would allow programs to detect when the compose key is pressed.
    There is already a key on the French keyboard that behaves this way. It is the key to the left of the '1' key and it has the pipe symbol on it. If you press Shift plus this pipe key, no character is produces but a keyPressed event with a keycode of 222 is produced. I merely point this out to show that there is a way to report key events whithout producing output on the screen.
    Thanks, Brian Bruderer

    I don't know if this actually helps, but it seems that you can bind an Action to a dead key like the circumflex of the French keyboard
    Keymap keymap = textPane.addKeymap("MyEmacsBindings", textPane.getKeymap());
    Action action = getActionByName(DefaultEditorKit.beginAction );
    KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_DEAD_CIRCUMFLEX);
    keymap.addActionForKeyStroke(key, action);I saw this on a Swing tutorial and modified it slightly. Have a look at it :
    http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html
    Chris

  • Xorg shutting down when certain keys are pressed

    Hello after some recent updates, I've experienced that xserver is shutting down and I need to restart my computer to get a working a system back.
    When I hold keys like backspace arrow keys this happens, anyone experienced the same lately?

    (II) config/hal: Adding input device Logitech Logitech BT Mini-Receiver
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Logitech Logitech BT Mini-Receiver
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device G15 Keyboard G15 Keyboard
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Gaming Keyboard
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Gaming Keyboard
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Logitech USB Gaming Mouse
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Macintosh mouse button emulation
    (EE) config/hal: NewInputDeviceRequest failed (8)
    I have disabled the hotplugging in my config.

  • Blackberry typing when no keys are pressed

    Hello,
    I have a BB Tour 9630. The problem that I am having is that every 5 seconds the letter "q" is typed on the screen without any buttons being pressed. This happens when I am using the browser, sending a text, trying to place a call and also when the blackberry is just setting on the desk. I have pulled the battery and also reset the blackberry and reloaded the software. Does anyone have any suggestions?
    Thanks,
    Ryan

    Hi and Welcome to the Forums!
    I suggest you contact your service provider for warranty evaluation.
    Good luck!
    Occam's Razor nearly always applies when troubleshooting technology issues!
    If anyone has been helpful to you, please show your appreciation by clicking the button inside of their post. Please click here and read, along with the threads to which it links, for helpful information to guide you as you proceed. I always recommend that you treat your BlackBerry like any other computing device, including using a regular backup schedule...click here for an article with instructions.
    Join our BBM Channels
    BSCF General Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • I have a 27" desktop mac running with OSX 10.6.8.  The bluetooth keyboard started not typing when some of the keys are pressed. Plugged in a wired keyboard, started doing the same thing.  Now the cursor moves but cannot select anything.  Suggestions?

    I have a 27" desktop mac running with OSX 10.6.8.  The bluetooth keyboard started not typing when some of the keys are pressed. Plugged in a wired keyboard, started doing the same thing.  When opening up a browser or application, the cursor moves but cannot select anything.  Suggestions?

    Maybe something in these Apple support articles will help.
    Keyboard Doesn’t Work
    Keyboard Keys Do Not Respond
    Keyboard/Mouse – Troubleshooting Wireless

  • My arrow keys are not working inside flash games

    Hello,
    Whenever I play any flash game, my arrow keys are not working. They work perfectly fine when I am scrolling the site with them, but not in flash games.
    I am using Google Chrome, flash player version 10,3,181,36 and my operating system is Win7 64 bit.
    Solutions I've already tried:
    Use another webbrowser
    Re-install Flash player
    Restart PC
    Try another keyboard
    I have this problem ever since the last Flash Player update.

    Hey there Questionair1.
    It sounds like your arrow keys are not working correctly. I would start with these steps from the following article named:
    OS X Mavericks: If keys on your keyboard don’t work
    http://support.apple.com/kb/PH13809
    You may have accidentally set an option that changes how your keyboard operates.
    Choose Apple menu > System Preferences, click Dictation & Speech, then click “Text to Speech.” If “Speak selected text when the key is pressed” is selected, deselect it or click Change Key to select another key.
    Choose Apple menu > System Preferences, click Accessibility, then click Keyboard. Make sure Slow Keys is turned off. If Slow Keys is on, you must hold down a key longer than usual before it’s recognized.
    Thank you for using Apple Support Communities.
    All the best,
    Sterling

  • Left and right arrow keys are not working properly now

    I searched the forum and didn't see this posted. My left and right arrow keys are not working properly now.
    For example, when typing, left arrow does not produce the desired action (move cursor one spot to the left), and right arrow does not produce the desired action (move cursor one spot to the right). Also, control + left no longer activates spaces, and control + right no longer activates spaces.
    Interestingly, in investigating the problem, I noticed that my shortcut keys for activating spaces and for activating expose had been changed (software update?). Before, I had set F5 (without pressing fn) for expose and had set F6 (without pressing fn) for spaces. Now, I noticed spaces was set back to the default of F8 (requiring fn).
    The issue persists after shut down, and even when logged on to other users, e.g. Guest.
    All other keys seem to be working fine, including the up and down arrows.
    I don't think it's a cleaning issue because fn + left + right produces the "[" character on the screen.
    Any ideas?

    I'm having the same type of issue on my MacBook except not with arrow keys. The volume keys do different things when I click on them. For example, the mute key brings up the dashboard and the volume up key makes all the open screens smaller (don't know what this feature is called). I did a software update then restarted the computer when this starting happening. Any help would be greatly appreciated, thanks.

  • Default button being clicked multiple times when enter key is pressed

    Hello,
    There seems to be a strange difference in how the default button behaves in JRE 1.4.X versus 1.3.X.
    In 1.3.X, when the enter key was pressed, the default button would be "pressed down" when the key was pressed, but wouldn't be fully clicked until the enter key was released. This means that only one event would be fired, even if the enter key was held down for a long time.
    In 1.4.X however, if the enter key is pressed and held for more than a second, then the default button is clicked multiple times until the enter key is released.
    Consider the following code (which is just a dialog with a button on it):
    public class SimpleDialog extends JDialog implements java.awt.event.ActionListener
    private JButton jButton1 = new JButton("button");
    public SimpleDialog()
    this.getContentPane().add(jButton1);
    this.getRootPane().setDefaultButton(jButton1);
    jButton1.addActionListener(this);
    this.pack();
    public void actionPerformed(ActionEvent e)
    if (e.getSource() == jButton1)
    System.out.println("button pressed");
    public static void main(String[] args)
    new SimpleDialog().show();
    When you compile and run this code under 1.3.1, and hold the enter key down for 10 seconds, you will only see one print line statement.
    However, if you compile and run this code under 1.4.1, and then hold the enter key down for 10 seconds, you will see about 100 print line statements.
    Is this a bug in 1.4.X or was this desired functionality (e.g. was it fixing some other bug)?
    Does anyone know how I can make it behave the "old way" (when the default button was only clicked once)?
    Thanks in advance if you have any advice.
    Dave

    Hello all,
    I think I have found a solution. The behaviour of the how the default button is triggered is contained withing the RootPaneUI. So, if I override the default RootPaneUI used by the UIDefaults with my own RootPaneUI, I can define that behaviour for myself.
    Here is my simple dialog with a button and a textfield (when the focus is NOT on the button, and the enter key is pressed, I don't want the actionPerformed method to be called until the enter key is released):
    package focustests;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    public class SimpleDialog extends JDialog implements java.awt.event.ActionListener
    private JButton jButton1 = new JButton("button");
    public SimpleDialog()
    this.getContentPane().add(new JTextField("a text field"), BorderLayout.NORTH);
    this.getContentPane().add(jButton1, BorderLayout.SOUTH);
    this.getRootPane().setDefaultButton(jButton1);
    jButton1.addActionListener(this);
    this.pack();
    public void actionPerformed(ActionEvent e)
    if (e.getSource() == jButton1)
    System.out.println("button pressed");
    public static void main(String[] args)
    javax.swing.UIManager.getDefaults().put("RootPaneUI", "focustests.MyRootPaneUI");
    new SimpleDialog().show();
    and the MyRootPaneUI class controls the behaviour for how the default button is handled:
    package focustests;
    import javax.swing.*;
    * Since we are using the Windows look and feel in our product, we should extend from the
    * Windows laf RootPaneUI
    public class MyRootPaneUI extends com.sun.java.swing.plaf.windows.WindowsRootPaneUI
    private final static MyRootPaneUI myRootPaneUI = new MyRootPaneUI();
    public static javax.swing.plaf.ComponentUI createUI(JComponent c) {
    return myRootPaneUI;
    protected void installKeyboardActions(JRootPane root) {
    super.installKeyboardActions(root);
    InputMap km = SwingUtilities.getUIInputMap(root,
    JComponent.WHEN_IN_FOCUSED_WINDOW);
    if (km == null) {
    km = new javax.swing.plaf.InputMapUIResource();
    SwingUtilities.replaceUIInputMap(root,
    JComponent.WHEN_IN_FOCUSED_WINDOW, km);
    //when the Enter key is pressed (with no modifiers), trigger a "pressed" event
    km.put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER,
    0, false), "pressed");
    //when the Enter key is released (with no modifiers), trigger a "release" event
    km.put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER,
    0, true), "released");
    ActionMap am = SwingUtilities.getUIActionMap(root);
    if (am == null) {
    am = new javax.swing.plaf.ActionMapUIResource();
    SwingUtilities.replaceUIActionMap(root, am);
    am.put("press", new HoldDefaultButtonAction(root, true));
    am.put("release", new HoldDefaultButtonAction(root, false));
    * This is a copy of the static nested class DefaultAction which was
    * contained in the JRootPane class in Java 1.3.1. Since we are
    * using Java 1.4.1, and we don't like the way the new JRE handles
    * the default button, we will replace it with the old (1.3.1) way of
    * doing things.
    static class HoldDefaultButtonAction extends AbstractAction {
    JRootPane root;
    boolean press;
    HoldDefaultButtonAction(JRootPane root, boolean press) {
    this.root = root;
    this.press = press;
    public void actionPerformed(java.awt.event.ActionEvent e) {
    JButton owner = root.getDefaultButton();
    if (owner != null && SwingUtilities.getRootPane(owner) == root) {
    ButtonModel model = owner.getModel();
    if (press) {
    model.setArmed(true);
    model.setPressed(true);
    } else {
    model.setPressed(false);
    public boolean isEnabled() {
    JButton owner = root.getDefaultButton();
    return (owner != null && owner.getModel().isEnabled());
    This seems to work. Does anyone have any comments on this solution?
    Tjacobs, I still don't see how adding a key listeners or overriding the processKeyEvent method on my button would help. The button won't receive the key event unless the focus is on the button. There is no method "enableEvents(...)" in the AWTEventMulticaster. Perhaps you have some code examples? Thanks anyway for your help.
    Dave

  • Bluetooth Wireless Keyboard acting as if keys are pressed

    I've had an Apple Aluminum Bluetooth keyboard for over two years, and last week (after a fresh install of OS X Lion on my late 2009 Mac Mini) all of a sudden the Mac is acting as if multiple keys are pressed when nothing is touching the keyboard. I have updated all software, replaced the batteries, deleted and re-paired the keyboard (which was a massive struggle as it acted as if keys were depressed even during the pairing process). Nothing has ever been spilled on the keyboard. The only way to make the sound of the "phantom depressed key" go away and regain full control of the computer, is to remove the batteries from the keyboard.
    Strangely, the problem seemed to have been resolved somehow after I placed the "Show character viewer" icon in the main menu (ie. where there is usually a US/other nationality flag). After doing so, all of a sudden the problem disappeared.
    Now, exactly a week later, the problem has reappeared.
    Can anyone explain why a keyboard would act this way? Has anyone had a similar problem? Is there a fix?
    I'm a longtime reader of theese support discussion boards, but this is the first time I've posted. Let me know if there's information that I can provide that would help us solve this issue.
    Cheers.

    I have similar issue: both left and right shift keys time to time being pressed, it disappears after a while...but once I couldn't log in as the shift option puts caps lock on....really annoying.
    The only reason in my case is the keyboard cleaning that I have done recently...
    There is no quick fix for this I am afraid.
    Options:
    1.Apple replaces the keyboard you pay around £200 I think....
    2. Buy new keyboard on Ebay and replace it yourself £40....
    http://www.ifixit.com/Guide/MacBook+Pro+13-Inch+Unibody+Mid+2010+Keyboard+Replac ement/23632#s61162
    Anyway lets see.

  • Solved My left and right arrow keys are not working

    I just installed gnome (been using kdemod) and now, while in gnome, my left and right arrow keys are not working. Please help me solve this. I am not using compiz.
    Last edited by patrickaupperle (2009-03-25 11:19:04)

    Hi!
    I had that problem with Debian and later Arch, after I messed something up (never found out what). There was a chaotic but easy cure though (I've got no idea, why it did that on 3 different installations the exact same way - might be a logitech-keyboard issue or something... me-as-user-related)?:
    - do all keyboard changes needed in xorg.conf NOW, had to repeat "strange steps" after each change.
    - add new keyboard layout (no changes to default) in gnome keyboard settings
    - delete old keyboard layout
    - browse trough the keyboard layout "previews" of the newly added layout until it crashes (this step might be optional *g*)
    - restart gnome, add "new new" layout, delete "new old" keyboard layout, and set the "new new" layout right (try evdev-manages first, then switch to youkeyboard/yourlocal if that didn't do it).
    good look!

  • How detect shift key release when both shift keys are pressed

    Why is no event fired when a shift key is released if both shift keys were pressed?
    1. Press left shift - keydown event
    2. Press right shift - keydown event
    3. Release left shift - no event
    4. Release right shift - keyup event
    So, we have two keydown events and just one keyup event.

    I think that the two shift keys are logically the same key and both provide VK_SHIFT. Since they are the same key, it's still down when the left key goes up and so no up fires. This behavour is inherited from the underlying keyboard input system and is the
    same as you'll see if you handle WM_KEYDOWN and WM_KEYUP in a Win32 app.
    You can differentiate the VirtualKey.LShift and RShift keys with CoreWindow.GetKeyState() or GetAsyncKeyState(), but there isn't a direct way to catch your state #3 in action.

  • IPhone letters don't pop up when keys are pressed

    You know how the letters pop up from under your finger when keys a pressed on the keyboard to let you know which key you press, well sometimes certain letters on my keyboard stop popping up. A restart fixes the key but a few minutes or hours (intermittent) later a different letter stops working. Anybody else have this problem?

    The problem seems to be related to the Auto-correct and Auto-capitalization feature. If I turn either one off (Settings->General->Keyboard), I don't have the problem but when both are on, it comes back. It also seems to affect the first character that I type in the input field.
    With both Auto features on, if I go to the login page of Twitter and type, say, a 'v' as the first character of the Username, a capital V is entered and the "V" pops up, but if I backspace, tap shift to go to lowercase and tap v again, the v does not pop up from the keyboard but it does show up in the username input field. I can reset things if I tap the Done button on the keyboard then tap in the username field to bring the keyboard back up. The problem is still there, but I can see it happen with a new letter if I want.
    Try it and post what you find. You don't have to have a Twitter account to see it happen and I'd be interested to know if it's consistent.

  • Up and down arrow keys are not working properly when using SQL CLI on Linux

    Every time I hit up, down, left, and right arrow keys to return to a previous sql command I got ^[[D, please any hints to fix this issue?
    The backspace works fine with me. I tried to use stty to fix this issue with no luck.
    Thanks
    Mohamed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Which SQL client and Linux version are we talking about?
    Assuming you are using the Oracle sqlplus client... it does not support command line editing and history using the arrow keys. For this to work you will have to install a product called Readline Wrapper. See my answer at the General Database forum:
    sqlplus command line editing

  • Satellite A300 - arrow keys are moving alone

    Hello,
    I have this strange problem with my laptop.
    The arrow keys move alone either up and down or left and right and they keep moving till i press many times on shift or alt or ctr.
    I need your help plz... and thanks in advance.
    Husam

    Hi
    Where did you notice this issue? Are any special applications running when this issue appears?
    To be honest Im not sure about this issue but usually something like that can be related to keyboard malfunction
    Therefore you should check if the keyboard needs to be replaced
    Try to connect an USB keyboard and check if the same would happen
    If not, then it's internal keyboard issue.

  • Equium A210 hangs on Post when any key is pressed

    Hi, I have a Equium A210-1C4 that has an odd error, when I boot the machine and don't touch the keyboard it loads into Windows fine and everything works as it should.
    If the machine shuts down unexpectdley,on restart it shows the common selection of safe mode and so on with the countdown timer, if I try and select one of the other options with the arrow key the timer stops and the machine hangs (as soon as I press any key)  I then have to power it off and allow it to boot without touching anything in order to get it working again.
    The same thing occures if I try and press  F8 on boot, it just hangs.  
    I can get into the BIOS settings and also select the boot order settings. The keyboard is functioning well within windows (no stuck keys) and I have reloaded the O/S and flashed the BIOS to try and resolve this.
    The Laptop had Vista loaded origianlly but now is Win 7 - both O/S's have had the same issue  
    Any ideas would be great 
    Thanks

    Since you traded keyboards and got the same results when booting into Windows, it sounds like a Windows issue. Running the Recovery disks would reimage the hard drive and probably fix the problem. However it would wipe out all of the programs, files and settings that you installed and set since you got the computer. It would fix the problem but could cost you a lot in time and preparation by having to backup all of your files, music and pictures. Plus you may need to let some of your program publishers know that you are doing a reimage so you can get your software reinstalled without having to pay a second time. Depending how much stuff you have to backup, this could be a very time consuming process to fix this one problem.

Maybe you are looking for

  • How to print 3 rows in same line in to smartforms

    Hi all experts and seniors..... I want to print three records in a same row of layout e.x No.  Date     Qty         No.  Date    Qty                  No.  Date     Qty 1      2007    10            2    2007   20                   3      2007      15

  • HT203242 iTunes for Windows: Optical drive is no longer recognized, or "Disc burner or software not found" alert after install.

    I have the solution. Uninstall Itunes. That is what I did and my disc burner works fine. I am gonna wait for Apple to come up with a solution. So if there is no solution that means no Itunes which means no Iphone or Ipod and time to move on to a coma

  • Where is the employees table in hr 10g xe database?

    when i connect to oracle 10g express edition (xe) through an application called crystal reports, i see an hr with tables that include employees, jobs, countries etc. however, when i use sqlplus from my c prompt and execute the following query: select

  • Trace file having ??? for report server's engine

    I'm facing problem regarding the font for trace file of report engine. Trace is showing ??? like the following. Can anybody help me how to change the language for the the trace only. Error 50103 (C Engine): ERR REP-0501: ??? ???? ??? ????? ?????? ???

  • Capacity Consumption in SNP planning book

    Hello, Can anyone explain what drives the calculation of capacity consumption KF in the planning book. Here is the example. 1) I have a Resource whose SNP bucket capacity of 90,000/Hr = 450000/Wk 2) I created a PP/DS PPM for one product from ECC and