2016-11-18 122 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是不可能的。