2016-11-18 118 views
0

我正在研究Windows和MacOS的屏幕键盘,并且我制作了一个小测试应用程序。它有一个按钮,并将字母“M”键入活动应用程序。它适用于Windows 10,但不适用于Mac(我正在运行macOS 10.12)。在macOS中,只要我按下按钮,无论我试图发送“M”失去焦点(文本输入光标消失)的应用程序,即使我的单键“键盘”全部都有setFocusable(false)这个地方。我也在按钮上尝试了自己的MouseListener。适用于Mac的Java虚拟键盘

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class main { 
    private static Robot robot; 
    private static Rectangle rectangle; 

    public static void main(String[] args){ 
     try { 
      robot = new Robot(); 
     } catch (AWTException e) { 
      e.printStackTrace(); 
     } 

     Button button = new Button("M"); 
     button.setFocusable(false); 

     JFrame frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(100, 100); 
     frame.add(button); 

     frame.setAlwaysOnTop(true); 

     //set everything I can think of to unfocusable!!! 
     frame.setFocusable(false); 
     frame.setAutoRequestFocus(false); 
     frame.setFocusableWindowState(false); 
     frame.getRootPane().setFocusable(false); 
     frame.setVisible(true); 

     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       sendKeystroke(); 
      } 
     }); 
     //Instead of adding a listener to the button, I've also tried my own MouseListener. 
/*  button.addMouseListener(new MouseTrap()); 
     rectangle = button.getBounds();*/ 
    } 

    private static void sendKeystroke(){ 
     robot.keyPress(KeyEvent.VK_M); 
     robot.keyRelease(KeyEvent.VK_M); 
    } 

    private static class MouseTrap extends MouseAdapter{ 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      if (rectangle.contains(e.getPoint())){ 
       sendKeystroke(); 
      } 
     } 
    } 
} 

看起来像macOS确实让一些应用程序有焦点,而不需要从另一个应用程序的光标。例如从系统托盘中进行VMware或Spotlight搜索。

Cursor for VMware and IntelliJ at the same time

我见过对方的回答是哪些非Java:

Virtual Keyboard Cocoa & Objective C

但我真的得走了所有的Java时,适用于Windows原生?除了学习曲线(Mac上没有任何本地功能),我想保持Win和Mac版本的版本尽可能接近。

任何人都知道我可以如何使用直接的Java工作? (注意:与上述链接提问者的情况一样,我不能只使用键盘视图,因为我想从键盘发送修改/附加数据,例如文本预测。我相信这将需要额外的本机代码再次。)

回答

0

我试图找到非常多的方式。但似乎没有任何本地代码,纯Java是不可能的。