计划exectution期间从未更新

计划exectution期间从未更新

本文介绍了Toolkit.getDefaultToolkit()。getLockingKeyState(...)计划exectution期间从未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我有一个 Toolkit.getDefaultToolkit()的问题。getLockingKeyState(...)从不更新。它正确地报告我第一次查询,然后当我使用键盘改变国家,改变永远不会体现出来。

这是由设计(似乎也不是那么的API文档),一个错误,或者是有什么与我的code?

下面是一个简短的,自包含的例子来说明这个问题:

 公共类LockingStateIssue {
    公共静态无效的主要(字串[] args){
        定时器定时器=新的Timer();
        timer.scheduleAtFixedRate(新的TimerTask(){
            @覆盖公共无效的run(){
                如果(Toolkit.getDefaultToolkit()。getLockingKeyState(KeyEvent.VK_NUM_LOCK)){
                    System.out.print(*);
                }其他{
                    System.out.print(。);
                }
            }
        },0,200);
    }
}

在运行时,我的Windows机器,使用Java 1.7.0_45上,它打印无论是 ....... *** ****** 取决于Num Lock键的初始状态,但从来没有像组合.. **。** 像我期待它,切换按钮时。


解决方案

  1. 有没有简单的Java正确的方式,而不,并重点在windows太,键盘记录器被阻挡在Java中,


  2. (不可能是主要的问题,但没有也会被显示出来)从util.Timer循环出EDT,更在的,工具包是从AWT组件,EDT问题,是大多数AWT包过


  3. 对于我的工作(在previous两分的情况下,要求得到满足。),用于测试目的,试图将焦点移至在Windows操作系统的另一个活动窗口durring这个程序执行


code

 进口java.awt.AWTEvent中;
进口java.awt.EventQueue中;
进口java.awt.Toolkit中;
进口java.awt.event.AWTEventListener;
进口java.awt.event.ActionEvent中;
进口java.awt.event.KeyEvent中;
进口javax.swing.AbstractAction中;
进口javax.swing.Action中;
进口javax.swing.JFrame中;公共类ToolkitAndNumLock {    私人javax.swing.Timer中的计时器= NULL;
    私人的JFrame框架=新的JFrame();    公共ToolkitAndNumLock(){
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,300);
        frame.setVisible(真);
        开始();
        //取消注释Toolkit.getXxx监听的KeyEvent,你可以(开始();)块SwingTimer
        //Toolkit.getDefaultToolkit().addAWTEventListener(listener,AWTEvent.KEY_EVENT_MASK);
    }
    AWTEventListener所监听器=新AWTEventListener所(){
        @覆盖
        公共无效eventDispatched(AWTEvent中的事件){
            如果(事件的instanceof的KeyEvent){
                柯的KeyEvent =(KeyEvent的)事件;
                如果(ke.getID()== KeyEvent.KEY_ preSSED){
                    如果(ke.getKey code()== KeyEvent.VK_CAPS_LOCK){
                        的System.out.println(CapsLock键pressed);
                    }
                    如果(ke.getKey code()== KeyEvent.VK_SCROLL_LOCK){
                        的System.out.println(ScrollLock键pressed);
                    }
                    如果(ke.getKey code()== KeyEvent.VK_NUM_LOCK){
                        的System.out.println(NumLock键pressed);
                    }
                }
            }
        }
    };    私人无效的start(){
        定时器=新javax.swing.Timer中(2500,updateCol());
        timer.setRepeats(真);
        timer.start();
    }    公益行动updateCol(){
        返回新AbstractAction(文字负荷行动){
            私有静态最后的serialVersionUID长1L =;
            私人布尔BOL = TRUE;            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                如果(Toolkit.getDefaultToolkit()。getLockingKeyState(KeyEvent.VK_NUM_LOCK)){
                    的System.out.println(真);
                }其他{
                    的System.out.println(假);
                }
                如果(BOL){
                    Toolkit.getDefaultToolkit()setLockingKeyState(KeyEvent.VK_NUM_LOCK,真)。
                }其他{
                    Toolkit.getDefaultToolkit()setLockingKeyState(KeyEvent.VK_NUM_LOCK,FALSE)。
                }
                BOL = BOL!;
            }
        };
    }    公共静态无效的主要(字符串ARGS []){
        可运行亚军=新的Runnable(){
            @覆盖
            公共无效的run(){
                新ToolkitAndNumLock();
            }
        };
        EventQueue.invokeLater(亚军);
    }
}

As the title says, I have the problem that Toolkit.getDefaultToolkit().getLockingKeyState(...) never updates. It reports correctly the first time I query it, then when I change the state using keyboard, the change is never reflected.

Is this by design (doesn't seem so in the API doc), a bug, or is there something with my code?

Here's a short, self-contained example to demonstrate the issue:

public class LockingStateIssue {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override public void run() {
                if (Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_NUM_LOCK)) {
                    System.out.print("*");
                } else {
                    System.out.print(".");
                }
            }
        }, 0, 200);
    }
}

When run, on my Windows machine, using Java 1.7.0_45, it prints either ....... or ********* depending on the initial state of the num lock key, but never a mix like ..**.** like I expect it to, when toggling the button.

解决方案
  1. there isn't a correct way in plain Java without visible Java container and with focus in windows too, KeyLoggers are blocked in Java,

  2. (could not be main issue, but nothing will be printed too) loop from util.Timer is out of EDT, more in Concurency in Swing, Toolkit is from AWT package, EDT issue is valid for most of AWT packages too

  3. work for me (in the case that previous two points, requirements are met..), for testing purpose to try move focus to another active window in Windows OS durring this program execution

code

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;

public class ToolkitAndNumLock {

    private javax.swing.Timer timer = null;
    private JFrame frame = new JFrame();

    public ToolkitAndNumLock() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setVisible(true);
        start();
        //uncomment Toolkit.getXxx listening a KeyEvents, you can (start();) block SwingTimer
        //Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
    }
    AWTEventListener listener = new AWTEventListener() {
        @Override
        public void eventDispatched(AWTEvent event) {
            if (event instanceof KeyEvent) {
                KeyEvent ke = (KeyEvent) event;
                if (ke.getID() == KeyEvent.KEY_PRESSED) {
                    if (ke.getKeyCode() == KeyEvent.VK_CAPS_LOCK) {
                        System.out.println("CapsLock Pressed");
                    }
                    if (ke.getKeyCode() == KeyEvent.VK_SCROLL_LOCK) {
                        System.out.println("ScrollLock Pressed");
                    }
                    if (ke.getKeyCode() == KeyEvent.VK_NUM_LOCK) {
                        System.out.println("NumLock Pressed");
                    }
                }
            }
        }
    };

    private void start() {
        timer = new javax.swing.Timer(2500, updateCol());
        timer.setRepeats(true);
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {
            private static final long serialVersionUID = 1L;
            private Boolean bol = true;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_NUM_LOCK)) {
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }
                if (bol) {
                    Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, true);
                } else {
                    Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, false);
                }
                bol = !bol;
            }
        };
    }

    public static void main(String args[]) {
        Runnable runner = new Runnable() {
            @Override
            public void run() {
                new ToolkitAndNumLock();
            }
        };
        EventQueue.invokeLater(runner);
    }
}

这篇关于Toolkit.getDefaultToolkit()。getLockingKeyState(...)计划exectution期间从未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:25