开发者

Java Swing Key Event base

开发者 https://www.devze.com 2023-03-02 18:13 出处:网络
In one of my menus, one menuIte开发者_运维技巧m\'s shortcut key is Ctrl+Greater code for that isKeyEvent.VK_GREATER, Event.CTRL_MASK

In one of my menus, one menuIte开发者_运维技巧m's shortcut key is

Ctrl+Greater

code for that is KeyEvent.VK_GREATER, Event.CTRL_MASK

but when I press Ctrl+Greater it's not working.... Can any one please suggest?


Thought this rather old nuisance had been fixed .. obviously not ;-)

In the ol' days, it was only safe to use keyStrokes which have VK_Something with Something being accessible without shift on an american keyboard layout. That means, using keybinding to keys which have positions (w/out shift or even via gr) different from that base, is either highly keyboard layout dependent or highly unstable - better don't use.

   // technical binding to "Greater" on a German keyboard  
   other.setAccelerator(KeyStroke.getKeyStroke("control shift LESS"));

   // BUT ... not really, its localized description is
   Str-Umschalt-Kleiner als


Check out my code. Here all is fine.

THIS example is not appropriate to the problem but I gave it first and (was nagged for it) thus it is left here for reference. For the appropriate one, i.e. setting accelerator on a menu item, please see EDIT1 below.

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class CtrlGreaterTestInaccurateAnswer extends JFrame
{
    private static final long serialVersionUID = 1L;
    private KeyListener kL = new KeyAdapter()
    {
        @Override
        public void keyReleased(KeyEvent e)
        {
            super.keyReleased(e);
        System.out.println("keyReleased KeyEvent.VK_GREATER=" + KeyEvent.VK_GREATER
                + "; e.getKeyCode()=" + e.getKeyCode()
                + "; e.getKeyChar()=" + e.getKeyChar()
                + "; e.isControlDown()=" + e.isControlDown()
                + "; e.isShiftDown()=" + e.isShiftDown() 
                + "; (int)'.'="+(int)'.' +"; (int)'>'="+(int)'>');
        if(e.isControlDown()    && e.getKeyChar() == '>')
            System.out.println("keyReleased ctrl + greater");
        }
    };

    public CtrlGreaterTestInaccurateAnswer()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addKeyListener(kL);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                CtrlGreaterTestInaccurateAnswer f = new CtrlGreaterTestInaccurateAnswer();
                f.setSize(400, 400);
                f.setVisible(true);
            }
        });
    }
}

From what I have observed KeyEvent.VK_GREATER returns 160, and the code returned when pressing '>' is 46, i.e. the same as pressing '.'. Thus, if you use the getKeyCode method for detection you have a problem here.

EDIT1 (Appropriate example): Setting accelerator for CTRL + >. Please notice that still the accelerator will say Ctrl+Shift-PERIOD (Ctrl+Shift+.)

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class CtrlGreaterTest extends JFrame
{
    private static final long serialVersionUID = 1L;

    public CtrlGreaterTest()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        JMenu menu1 = new JMenu("Menu1");
        JMenuItem menuItem1 = new JMenuItem();
        menuItem1.setAction(onCtrlGreaterAction);
        menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 
                KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK));         
        //or use this
        //menuItem1.setAccelerator(KeyStroke.getKeyStroke("ctrl shift PERIOD"));
        menu1.add(menuItem1);

        JMenuItem menuItem2 = new JMenuItem();
        menuItem2.setAction(onCtrlAAction);
        menuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 
                KeyEvent.CTRL_MASK)); 

        System.out.println("KeyEvent.VK_PERIOD| KeyEvent.SHIFT_MASK="
                +(KeyEvent.VK_PERIOD| KeyEvent.SHIFT_MASK)
                + "; KeyEvent.VK_GREATER="+(KeyEvent.VK_GREATER) 
                + "; (int)'>'="+(int)'>');
        menu1.add(menuItem2);       
        menuBar.add(menu1);
        setJMenuBar(menuBar);
    }
    private AbstractAction onCtrlGreaterAction = new AbstractAction("CTRL + > Action")
    {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("onCtrlGreaterAction actionPerformed OI");
        }
    };

    private AbstractAction onCtrlAAction = new AbstractAction("CTRL + a Action")
    {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("onCtrlAAction actionPerformed AI");
        }
    };

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                CtrlGreaterTest f = new CtrlGreaterTest();
                f.setSize(400, 400);
                f.setVisible(true);
            }
        });
    }
}

Hope this is it :)

0

精彩评论

暂无评论...
验证码 换一张
取 消