Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify "Alt +" keypress, properly and reliably?

Tags:

java

keyboard

awt

My question is actually more general, but I'm using the action of a user holding down the "Alt" key, and pressing "+", as an example that shows the difficulties.

I'm working an a US English keyboard, which has the "=" (lowercase) and "+" (uppercase) on the same key, so to press "Alt +" (as might be indicated in a menu entry), I have to actually press "Alt Shift =". In Java AWT, pressing "Alt Shift =" generates a key-pressed KeyEvent with the keycode associated with the "=" key, and a key-typed KeyEvent containing the "±" character. So there is no obvious, reliable way of programatically deciding that "Alt" was held down while the "+" key was pressed.

I could do some mapping internally to fix this, such as mapping "±" to "Alt +", or mapping "Shift {keycode for = }" to "+". However, there don't seem to be any guarantees that this would work across different keyboard layouts; and it certainly isn't good coding style.

If anyone can suggest a way around these problems, or perhaps point be to code that's already handled this difficulty, I'd be most appreciative.

Thanks.

like image 269
Kenneth McDonald Avatar asked Nov 05 '22 00:11

Kenneth McDonald


1 Answers

Try this:

if(e.isAltDown())
{
    switch(e.getKeyChar())
    {
        case '+':
            System.out.println("Plus");
        break;
    }
}

Where e is the KeyEvent and it is handled in keyPressed method.

The above code will print Plus when you press ALT+Shift+= on the keyboard specified by you.

For complete working code see the below example:

import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.UIManager;


public class SwingTest 
{

    private static JFrame frame;

    public static void main(String[] args) throws Exception
    {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        frame = new JFrame("Event Test");

        Toolkit tk = Toolkit.getDefaultToolkit();  
        int xSize = ((int) tk.getScreenSize().getWidth()/2) + 100;  
        int ySize = ((int) tk.getScreenSize().getHeight()/2) + 50;  

        frame.setSize(xSize,ySize); 
        frame.setLocationRelativeTo(null); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.addKeyListener(new KeyListener() 
        {
            public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
            }

            public void keyPressed(KeyEvent e) 
            {
                if(e.isAltDown())
                {
                    switch(e.getKeyChar())
                    {
                    case '+':
                        System.out.println("Plus");
                        break;
                    }
                }
            }
        });

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(true);
            }
        });         
    }
}

Hope this will help.

like image 115
Favonius Avatar answered Nov 09 '22 09:11

Favonius