在键盘上打字会影响网络上的键盘

时间:2013-12-09 07:52:21

标签: java javascript java-ee

我正在为我的大学项目打造导师网络应用程序。我希望键盘显示在网站上,如果我在键盘上键入任何应该突出显示网页键盘键的字母。那么我应该使用什么技术在网上显示键盘。我想用java。

感谢任何帮助

提前致谢。

2 个答案:

答案 0 :(得分:0)

您需要为此实现Key Listeners。

键事件表示用户在键盘上键入的时间。具体而言,当用户按下或释放键盘键时,具有键盘焦点的组件触发键事件。请注意,只有当您的应用程序具有系统焦点时,这些事件才会起作用。

1) Make sure the component's isFocusable method returns true. This state allows the component to receive the focus. For example, you can enable keyboard focus for a JLabel component by calling the setFocusable(true) method on the label.
2) Make sure the component requests the focus when appropriate. For custom components, implement a mouse listener that calls the requestFocusInWindow method when the component is clicked

关键事件监听器的示例代码:

public class KeyEventDemo ...  implements KeyListener ... {
    ...//where initialization occurs:
        typingArea = new JTextField(20);
        typingArea.addKeyListener(this);

        //Uncomment this if you wish to turn off focus
        //traversal.  The focus subsystem consumes
        //focus traversal keys, such as Tab and Shift Tab.
        //If you uncomment the following line of code, this
        //disables focus traversal and the Tab events 
        //become available to the key event listener.
        //typingArea.setFocusTraversalKeysEnabled(false);
    ...
    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
        displayInfo(e, "KEY TYPED: ");
    }

    /** Handle the key-pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
        displayInfo(e, "KEY PRESSED: ");
    }

    /** Handle the key-released event from the text field. */
    public void keyReleased(KeyEvent e) {
        displayInfo(e, "KEY RELEASED: ");
    }
    ...
    private void displayInfo(KeyEvent e, String keyStatus){

        //You should only rely on the key char if the event
        //is a key typed event.
        int id = e.getID();
        String keyString;
        if (id == KeyEvent.KEY_TYPED) {
            char c = e.getKeyChar();
            keyString = "key character = '" + c + "'";
        } else {
            int keyCode = e.getKeyCode();
            keyString = "key code = " + keyCode
                    + " ("
                    + KeyEvent.getKeyText(keyCode)
                    + ")";
        }

        int modifiersEx = e.getModifiersEx();
        String modString = "extended modifiers = " + modifiersEx;
        String tmpString = KeyEvent.getModifiersExText(modifiersEx);
        if (tmpString.length() > 0) {
            modString += " (" + tmpString + ")";
        } else {
            modString += " (no extended modifiers)";
        }

        String actionString = "action key? ";
        if (e.isActionKey()) {
            actionString += "YES";
        } else {
            actionString += "NO";
        }

        String locationString = "key location: ";
        int location = e.getKeyLocation();
        if (location == KeyEvent.KEY_LOCATION_STANDARD) {
            locationString += "standard";
        } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
            locationString += "left";
        } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
            locationString += "right";
        } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
            locationString += "numpad";
        } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
            locationString += "unknown";
        }

        ...//Display information about the KeyEvent...
    }
}

尝试使用Java Docs/ tutorials获取更多帮助。

答案 1 :(得分:0)

网页最终在客户端浏览器上运行,客户端浏览器只能识别HTML,CSS和Javascript。您只能使用Javascript完成此操作,没有服务器端代码。

如果您真的想通过Java代码进行游戏,我可以建议两种(多种)方法:

  • 开发一个与JSP页面一起使用的taglib。 Taglib会在屏幕上打印虚拟键盘,并附带一些javascript代码,会听取键盘事件并在虚拟键盘上突出显示它们(这更像是一种混合解决方案)。

  • 使用Google的GWT。它可以让你完全用java代码创建你的虚拟键盘和事件监听器,并产生一个网页,而不需要你开发HTML,CSS和Javascript。 GWT基本上为您提供了一种构建网页的方法,类似于创建Swing GUI,并将其转换为HTML,CSS和Javascript代码。

http://code.google.com/p/google-web-toolkit
http://www.gwtproject.org