如何识别“Alt +”按键是否正确、可靠?

发布于 2024-10-26 18:55:05 字数 440 浏览 5 评论 0原文

我的问题实际上更笼统,但我使用用户按住“Alt”键并按“+”的操作作为显示困难的示例。

我正在使用美国英语键盘,该键盘在同一个键上有“=”(小写)和“+”(大写),因此要按“Alt +”(如菜单条目中所示),我必须实际按下“Alt Shift =”。在 Java AWT 中,按“Alt Shift =”会生成一个按键式 KeyEvent,其中的键码与“=”键相关联,以及一个包含“±”字符的键类型式 KeyEvent。因此,没有明显、可靠的方法来以编程方式确定按下“+”键时按住“Alt”。

我可以在内部进行一些映射来解决此问题,例如将“±”映射到“Alt +”,或将“Shift {keycode for = }”映射到“+”。然而,似乎没有任何保证这可以在不同的键盘布局上工作;这当然不是好的编码风格。

如果有人可以提出解决这些问题的方法,或者指出已经解决了这个困难的代码,我将不胜感激。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2024-11-02 18:55:05

试试这个:

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

其中 eKeyEvent 并且它在 keyPressed 方法中处理。

当您在指定的键盘上按 ALT+Shift+= 时,上面的代码将打印 Plus

有关完整的工作代码,请参阅下面的示例:

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);
            }
        });         
    }
}

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文