如何全局获取当前键盘状态? (即,无论查询应用程序是否关注焦点,当前都按下了哪些键) [英] How do you get the current keyboard state globally? (i.e. what keys are currently depressed regardless of if the inquiring application has focus)

查看:82
本文介绍了如何全局获取当前键盘状态? (即,无论查询应用程序是否关注焦点,当前都按下了哪些键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个屏幕捕获实用程序,并且希望能够在拍摄屏幕快照时存储键盘和鼠标的当前状态.

I'm writing a screen capture utility, and I'd like to be able to store the current state of the keyboard and mouse whenever a screenshot is taken.

使用鼠标执行此操作很简单,因为使用相关问题中描述的方式,您可以使用%29"rel =" nofollow noreferrer> PointerInfo 类为您提供当前鼠标位置的屏幕坐标和单击信息(如果需要).但是,我无法为该类键盘找到类似的模拟物.所有与键盘相关的类似乎都是针对特定焦点的.

Doing this for the mouse is simple, since using the PointerInfo class in a manner described in this related question gives you the screen coordinates for the current mouse location and the click information if desired. However, I haven't been able to find an analog to this class for the keyboard; all the keyboard related classes appear to be focus specific.

那么,有没有办法获取Java中当前的键盘状态?

So, is there a way to get the current keyboard state in Java?

P.S.请记住,我在找一个函数来检索有关按下了什么键的信息,而不是不是此类按下事件的监听器.

P.S. Remember that I'm looking for a function to call to retrieve the information regarding what keys are depressed, not a listener for such depression events.

推荐答案

您可以做的是实现KeyListener接口,并为您感兴趣的所有键赋予状态.

Something you can do is implement the KeyListener interface and give states to all the keys you're interested in.

例如,如果您有兴趣检查在屏幕截图上是否按下了箭头键,则可以实现此KeyListener界面并覆盖keyPressed()keyReleased()方法,并为这些键设置状态对keyPressedkeyReleased感兴趣.取决于事件.这样,当屏幕截图出现时,您只需读取这些键的状态即可

If you're interested in checking if the arrow keys are depressed upon a screenshot, for instance, you can implement this KeyListener interface and override keyPressed() and keyReleased() methods and set the state for those keys you are interested in to keyPressed or keyReleased. Depending on the event. That way, when the screenshot occurs, you can just read the state of those keys

如果您需要此解决方案是全球性的,而与应用程序无关,则可以在C中编写一个小钩子,该钩子可以与Java Native Interface集成以侦听关键事件. Java不允许您在不将侦听器附加到组件并且该组件具有焦点的情况下侦听关键事件.看看 JNativeHook .

If you need this solution to be global, regardless of application focus, you could write a small hook in C that you can integrate with Java Native Interface to listen for key events. Java doesn't let you listen to key events without you attaching the listener to a component and that component having focus. Have a look at JNativeHook.

如果只在应用程序具有焦点但在每个组件上都需要它时,则可以将监听器优雅地附加到所有组件上,或者可以编写自己的自定义KeyEventDispatcher并将其注册在KeyBoardFocusManager上.这样,只要您的应用程序具有焦点,无论具有特定焦点的组件是什么,您都可以捕获所有键盘事件.参见:

If you just need it when your application has focus but on every component you could inelegantly attach the listener to all your components or you could write your own custom KeyEventDispatcher and register it on the KeyBoardFocusManager. That way, as long as your application has focus, regardless of component that has specific focus, you could catch all keyboard events. See:

public class YourFrame extends JFrame {    

    public YourFrame() {
        // Finish all your layout and add your components
        //

        // Get the KeyboardFocusManager and register your custom dispatcher
        KeyboardFocusManager m = KeyboardFocusManager.getCurrentKeyboardFocusManager();

        m.addKeyEventDispatcher(new YourDispatcher());
    }

    private class YourDispatcher implements KeyEventDispatcher {
        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_TYPED) {
                // Do something to change the state of the key
            } else if (e.getID() == KeyEvent.KEY_PRESSED) {
                // Do something else
            }
            return false;
        }
    }

    public static void main(String[] args) {
        YourFrame yF = new YourFrame();
        yF.pack();
        yF.setVisible(true);
    }
}

这篇关于如何全局获取当前键盘状态? (即,无论查询应用程序是否关注焦点,当前都按下了哪些键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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