停止OSX变音符号为所有用户禁用Java中的键绑定吗? [英] Stop OSX diacritics disabling KeyBindings in Java for all users?

查看:59
本文介绍了停止OSX变音符号为所有用户禁用Java中的键绑定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:我知道此问题用户必须输入终端命令来解决此问题,但我希望可以将解决方案放入应用程序中的解决方案.)

(Note: I am aware of this question in which the user would have to enter a Terminal command to fix this issue, but I would prefer a solution in which the solution can be put into the application.)

为了说明,我在Java应用程序中使用了KeyBindings.但是,如果有人按住a,e,i,o,u,n,s等键,则变音菜单OSX将以某种方式完全禁用键输入.

To explain, I am using KeyBindings in a Java application; however, if one holds a key like a, e, i, o, u, n, s, etc., the diacritic menu OSX uses somehow completely disables key input.

但是,如果相关,它不会影响鼠标输入.

It does not, however, affect mouse input if that is relevant.

这是一些示例代码,可以在其中演示问题.如果在OSX上按住上述任何一个键约一秒钟或更长时间,则KeyBindings会完全停止工作. (不过,为了确保更多,我建议按住该键.

Here is some sample code in which the problem can be demonstrated. If you press and hold any of the aforementioned keys on OSX for approximately one second or more, the KeyBindings stop working entirely. (I recommend holding the key down for more just to be sure, though.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class Test extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private static JFrame frame;

    private static boolean up = false, down = false, left = false, right = false;

    private static int x = 275, y = 275;

    public static void main(String[] args) {
        Test t = new Test();
        t.setBounds(0, 0, 1200, 600);
        t.setVisible(true);

        Timer repaintTimer = new Timer(2, t);

        frame = new JFrame();
        frame.setSize(600, 600);

        setUpKeyActions(t);

        frame.add(t);

        Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocation((dim.width - frame.getWidth()) / 2, (dim.height - frame.getHeight()) / 2);
        frame.getContentPane().setLayout(null);
        frame.setAlwaysOnTop(true);
        frame.setResizable(false);

        repaintTimer.start();

        frame.setVisible(true);
        frame.requestFocus();
    }

    private static void setUpKeyActions(Test t) {
        int condition = WHEN_IN_FOCUSED_WINDOW;

        new KeyAction(t, condition, KeyEvent.VK_UP, 0, false) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                up = true;
            }

        };

        new KeyAction(t, condition, KeyEvent.VK_UP, 0, true) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                up = false;
            }

        };

        new KeyAction(t, condition, KeyEvent.VK_LEFT, 0, false) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                left = true;
            }

        };

        new KeyAction(t, condition, KeyEvent.VK_LEFT, 0, true) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                left = false;
            }

        };

        new KeyAction(t, condition, KeyEvent.VK_RIGHT, 0, false) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                right = true;
            }

        };

        new KeyAction(t, condition, KeyEvent.VK_RIGHT, 0, true) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                right = false;
            }

        };

        new KeyAction(t, condition, KeyEvent.VK_DOWN, 0, false) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                down = true;
            }

        };

        new KeyAction(t, condition, KeyEvent.VK_DOWN, 0, true) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                down = false;
            }

        };

    }

    private static abstract class KeyAction extends AbstractAction {
        private static final long serialVersionUID = 1L;

        KeyAction(JComponent component, int condition, int keyCode, int modifiers, boolean onKeyRelease) {
            InputMap inputMap = component.getInputMap(condition);
            ActionMap actionMap = component.getActionMap();
            KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifiers, onKeyRelease);
            inputMap.put(keyStroke, keyStroke.toString());
            actionMap.put(keyStroke.toString(), this);
        }

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(up)
            y -= 1;
        if(down)
            y += 1;
        if(right)
            x += 1;
        if(left)
            x -= 1;
        if(x < 0)
            x = 0;
        else if(x > frame.getWidth() - 30)
            x = frame.getWidth() - 30;
        if(y < 0)
            y = 0;
        else if(y > frame.getHeight() - 50)
            y = frame.getHeight() - 50;
        g.drawRect(x, y, 30, 30);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        frame.repaint();
    }

}

推荐答案

要回答我遇到的问题,问题是Java版本.

To answer the question I was having, the issue was the Java version.

我有JDK 1.8.0_151,但是显然这个问题在某些迭代中已得到修复,直到JDK 1.8.0_172.

I had JDK 1.8.0_151, but apparently this issue was fixed in some iteration up to JDK 1.8.0_172.

因此,为确保我的应用程序用户不会发生此问题,我将需要使用org.apache.commons.lang3.SystemUtils的JDK 1.9或更高版本,并使用

So, to make sure this issue does not happen to the users of my application, I will require JDK 1.9 or above using org.apache.commons.lang3.SystemUtils and use the isJavaVersionAtLeast(JavaVersion requiredVersion) method from this answer.

更新:

我实际上计划使用从问题中获得的方法此处,因为它使我不必要求JRE 1.9,但允许我至少设置1.8.0_172.

I actually plan to use the method I got from my question here since it allows me not to have to require JRE 1.9 but instead allows me to make the minimum 1.8.0_172.

这篇关于停止OSX变音符号为所有用户禁用Java中的键绑定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆