简体   繁体   English

Java Applet 中的键盘输入

[英]Keyboard input in Java Applet

What is the best way to listen for keyboard input in a Java Applet?在 Java Applet 中监听键盘输入的最佳方式是什么?

I have an applet which opens a JFrame and I am using a KeyListener to listen for keyboard input.我有一个打开 JFrame 的小程序,我使用 KeyListener 来监听键盘输入。 This works fine in my development environment (eclipse), but when I run the applet through a browser (I have tried Firefox and IE) it does not respond to keyboard events.这在我的开发环境 (eclipse) 中运行良好,但是当我通过浏览器运行小程序时(我尝试过 Firefox 和 IE),它不响应键盘事件。 However, if I run the applet and then minimize and maximize the frame, it works.但是,如果我运行小程序,然后最小化和最大化框架,它就可以工作。 I have tried setting focus to the JFrame in many different ways and also programmatically minimizing and maximizing it, but to no effect.我尝试以多种不同的方式将焦点设置到 JFrame,并以编程方式最小化和最大化它,但没有效果。 I have also tried key bindings, but with the same problem.我也试过键绑定,但有同样的问题。

I have trimmed my code down to the barest essentials of the problem and pasted it below.我已将我的代码精简到问题的最基本要素并将其粘贴在下面。 Can someone see what I am doing wrong or suggest a better solution?有人可以看到我做错了什么或提出更好的解决方案吗?

public class AppletTest extends Applet 
{    
    private GuiTest guiTest; 

    public void init() {
        guiTest = new GuiTest();
        final AppletTest at = this;
        guiTest.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent ke) {
                at.keyPressed(ke);
            }
            public void keyReleased(KeyEvent ke) {}
            public void keyTyped(KeyEvent e) {}             
        });
    }

    private void keyPressed(KeyEvent ke)
    {
        System.out.println("keyPressed "+KeyEvent.getKeyText(ke.getKeyCode()));
        getGuiTest().test(KeyEvent.getKeyText(ke.getKeyCode()));
    }
}

public class GuiTest extends JFrame {
    String teststring = "?";
    public GuiTest()
    {
        setSize(100,100);
        setEnabled(true);
        setVisible(true);
        setFocusable(true);
        requestFocus();
        requestFocusInWindow();
        toFront();
    }

    public void test(String t)
    {
        teststring = t;
        repaint();
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        g.drawString(teststring, 50, 50);
    }
}

I solved the problem.我解决了这个问题。 If I create the JFrame following a button press or mouse event on the applet, the key listener on the JFrame works.如果我在小程序上按下按钮或鼠标事件后创建 JFrame,则 JFrame 上的键侦听器会起作用。 Apparently, creating the frame from Applet.init() means that key listeners do not function correctly when opened through a browser.显然,从 Applet.init() 创建框架意味着关键侦听器在通过浏览器打开时无法正常工作。

However, the question remains - why?然而,问题仍然存在——为什么? If someone can explain this, I would greatly appreciate it.如果有人可以解释这一点,我将不胜感激。

I thought it might be because the frame should be created on the event dispatch thread, but using SwingUtilities.invokeLater or invokeAndWait did not work.我认为这可能是因为应该在事件调度线程上创建框架,但是使用 SwingUtilities.invokeLater 或 invokeAndWait 不起作用。

I think you are running into the plugin focus issue: in many modern browser a plugin only gains focus through either the user clicking on it or using Javascript.我认为您遇到了插件焦点问题:在许多现代浏览器中,插件只能通过用户单击或使用 Javascript 来获得焦点。 This typically affects Flash but it might be that it also affects applets.这通常会影响 Flash,但也可能会影响小程序。 Try Adobe's recommendations at http://kb2.adobe.com/cps/155/tn_15586.html .http://kb2.adobe.com/cps/155/tn_15586.html尝试 Adob​​e 的建议。

Let me know if that works for you.让我知道这是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM