2012-04-03 29 views
6

Mỗi khi tôi sử dụng Robot để di chuyển chuột, nó sẽ đặt lại tốc độ chuột Windows. Điều này thực sự gây phiền nhiễu để đối phó với, và tôi đã tự hỏi nếu có ai biết làm thế nào để sửa lỗi này. Đây là mã cơ bản mà tôi đang làm rối tung xung quanh với:Java Awt Robot thay đổi tốc độ chuột của Windows

Robot robot = new Robot(); 
robot.mouseMove(10, 1070); 
robot.delay(300); 
robot.mousePress(InputEvent.BUTTON1_MASK); 
robot.mouseRelease(InputEvent.BUTTON1_MASK); 
robot.delay(300); 
robotType("notepad"); 
robot.keyPress(KeyEvent.VK_ENTER); 
robot.keyRelease(KeyEvent.VK_ENTER); 
robot.delay(400); 
robotType("I am writing this."); 

Điều này có nghĩa là bấm nút khởi động, gõ "notepad", mở notepad, rồi gõ "Tôi đang viết".

robotType() chỉ là một hàm nhanh mà tôi đã thực hiện để chuyển đổi chuỗi thành một chuỗi các lần nhấn/phát hành bàn phím.

Trả lời

1

Điều này có vẻ như là lỗi cửa sổ, vì không có gì bạn đã thực hiện làm thay đổi tốc độ chuột. Có vẻ như bạn có thể gặp may ...

0

Không phải là một sửa chữa, nhưng một workaround:

Với JNA bạn có thể nhận/đặt chuột tốc độ (xác minh bạn đang chạy trên cửa sổ). Khi chương trình của bạn bắt đầu, hãy đọc tốc độ chuột. Sau đó, sau mỗi robot.mouseMove() sẽ khôi phục giá trị đó.

Bạn sẽ cần phải thêm jna.jarjna-platform.jar có thể tìm thấy ở đây: https://github.com/java-native-access/jna/tree/master/dist

interface User32 extends com.sun.jna.platform.win32.User32 { 

    User32 INSTANCE = (User32) Native.loadLibrary(User32.class, 
      W32APIOptions.DEFAULT_OPTIONS); 

    boolean SystemParametersInfo(
      int uiAction, 
      int uiParam, 
      Object pvParam, // Pointer or int 
      int fWinIni 
    ); 
} 

public static void main(String[] args) throws AWTException { 
    Pointer mouseSpeedPtr = new Memory(4); 
    Integer mouseSpeed = User32.INSTANCE.SystemParametersInfo(0x0070, 0, mouseSpeedPtr, 0) 
      ? mouseSpeedPtr.getInt(0) : null; 

    //[...] 

    rob.mouseMove(10, 1070); 
    if (mouseSpeed != null) { 
     User32.INSTANCE.SystemParametersInfo(0x0071, 0, mouseSpeed, 0x02); 
    } 

    //[...] 
} 
Các vấn đề liên quan