1

Can I have a Java program that listens to the keyboard without having a Component?

Can I use the Interface KeyListener without having a visual component?

I don't need any of the functionality in java.awt. I don't need any windows/components or such. I just want the program to react to some input.

4

3 回答 3

1

我可以有一个没有组件的 Java 程序来听键盘吗?

不。

我可以在没有可视组件的情况下使用接口 KeyListener 吗?

不。

于 2013-10-31T12:34:32.727 回答
1

必须有一些东西可以接受输入,以便调用 KeyListener。您向组件注册 KeyListener。需要某种输入组件,以便您可以注册 KeyListener。

请记住,您的代码位于 JVM 内部。JVM 是从底层操作系统(而不是您的代码)获得直接信号的那个。

您可以尝试在一些本机代码中创建一个 JNI 挂钩。

于 2013-10-31T12:37:18.870 回答
0

关于“<em>我可以在没有可视组件的情况下使用接口 KeyListener 吗?”</p>

是的,您可以(如果问题本身是问的):

package igb.so.SO19706590;

import static java.lang.System.out;

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

class KeyEventProducerConsumerSample {

    public static void main( final String[] args ) {

        final KeyEventProducerConsumerSample kes = new KeyEventProducerConsumerSample();
        final KeyEventProducer kep = kes.new KeyEventProducer();
        final KeyEventConsumer kec = kes.new KeyEventConsumer();

        kep.addKeyListener( kec ).raise();
    } // main()

    class KeyEventProducer {

        KeyListener kl;

        KeyEventProducer addKeyListener( final KeyListener kl ) {

            this.kl = kl;
            return this;
        } // addKeyListener()

        /** Creating KeyEvents programmatically, with no keyboard involved
         * and informing the KeyEventConsumer (KeyListener) about them.
         */
        void raise() {

            kl.keyTyped( new KeyEvent( new TextField(), 1, 1, 1, 1, '1' ) );
            kl.keyPressed( new KeyEvent( new TextField(), 2, 2, 2, 2, '2' ) );
            kl.keyReleased( new KeyEvent( new TextField(), 3, 3, 3, 3, '3' ) );
        } // raise()

    } // KeyEventProducer

    /** Using KeyListener interface without any visual component.
     */
    class KeyEventConsumer implements KeyListener {

        @Override
        public void keyTyped( final KeyEvent ke ) {
            out.println( ke );
        }

        @Override
        public void keyPressed( final KeyEvent ke ) {
            out.println( ke );
        }

        @Override
        public void keyReleased( final KeyEvent ke ) {
            out.println( ke );
        }

    } // KeyEventConsumer

} // KeyEventProducerConsumerSample

输出

java.awt.event.KeyEvent[unknown type,keyCode=1,keyText=Unknown keyCode: 0x1,keyChar='1',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0] on textfield0
java.awt.event.KeyEvent[unknown type,keyCode=2,keyText=Unknown keyCode: 0x2,keyChar='2',modifiers=Ctrl,extModifiers=Ctrl,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0] on textfield1
java.awt.event.KeyEvent[unknown type,keyCode=3,keyText=Cancel,keyChar='3',modifiers=Ctrl+Shift,extModifiers=Ctrl+Shift,keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0] on textfield2

于 2020-12-09T22:08:50.330 回答