如何在Cocoa中以编程方式模拟鼠标单击而不移动鼠标 [英] How to programmatically simulate a mouse click without moving mouse in Cocoa

查看:346
本文介绍了如何在Cocoa中以编程方式模拟鼠标单击而不移动鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣在Mac OS X上模拟鼠标点击事件/键盘敲击,而不实际移动鼠标。



在Windows中,可以使用



win32:模拟一个点击而不模拟鼠标移动?



Mac OS X有类似的吗?我知道 Quartz Event Services ,但似乎该API只会限制我将事件发送到当前键窗口。这是真的?是否可以将键盘/鼠标事件发送到非键窗口?我真的只需要能够发送一个键盘命令到另一个应用程序。

解决方案

我知道这是一个老线程,我想张贴一个答案,以防万一任何人绊倒这一点。



这两个最好的方法是完全独立于平台。 (除非你觉得使用C,这在我看来是不必要的冗长,但如果你想使用它看到这个答案



1)最简单的方法是使用 javAuto 。 (注意:javAuto已移动此处)。它几乎是一个java程序,可以编译和运行基本的自动化脚本跨平台。为了模拟鼠标点击,您可以使用以下命令:



mouseClick(button,x,y); p>

对于模拟mouseClick而不移动鼠标,您可以使用以下命令:

  //获取光标坐标
int cPos [] = cursorGetPos();

//鼠标点击你想要的坐标
mouseClick(left,x,y);

//立即将鼠标移回
mouseMove(cPos [0],cPos [1]);

由于mouseClick和mouseMove命令不涉及任何临时鼠标移动,

2)下一个最好的方法是使用普通的Java ,这将涉及比javAuto中的相同进程多一点的代码。

  import java.awt。*; 
import java.awt.event.InputEvent;

public class example {
public static void main(String [] args){
//获取当前坐标
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int xOrig =(int)b.getX();
int yOrig =(int)b.getY();

try {
//模拟点击10,50
Robot r = new Robot();
r.mouseMove(10,50);
r.mousePress(InputEvent.BUTTON1_MASK); //按下鼠标左键
r.mouseRelease(InputEvent.BUTTON1_MASK); //释放鼠标左键

//将鼠标移回原来的位置
r.mouseMove(xOrig,yOrig);
} catch(Exception e){
System.out.println(e.toString());
}
}
}


I am interested in simulating a mouse click event/keyboard stroke on Mac OS X without actually moving the mouse.

In Windows, it is possible to do this using messages:

win32: simulate a click without simulating mouse movement?

Is there an analog of this for Mac OS X? I am aware of Quartz Event Services, but it seems that that API would only restrict me to sending events to the current key window. Is this true? Is it possible to send keyboard/mouse events to non-key windows? I really just need to be able to send a keyboard command to another app.

解决方案

I know this is an old thread, but I wanted to post an answer just in case anybody stumbles upon this.

The two best ways to do this are actually completely platform independent. (Unless you feel like using C, which is unnecessarily verbose for this in my opinion, but if you want to use it see this answer)

1) The easiest way is to use javAuto. (Note: javAuto has moved here). It's pretty much a java program the can compile and run basic automation scripts cross platform. For simulating a mouse click you can use this command:

mouseClick(button, x, y);

For simulating a mouseClick without moving the mouse you can use this:

// get cursor coordinates
int cPos[] = cursorGetPos();

// mouse click at the coordinates you want
mouseClick("left", x, y);

// instantly move the mouse back
mouseMove(cPos[0], cPos[1]);

Since the mouseClick and mouseMove commands don't involve any interim mouse movements, a click will happen at (x, y) but the mouse won't move at all.

2) The next best way to do this is to use regular Java, which will involve a bit more code than the same process in javAuto.

import java.awt.*;
import java.awt.event.InputEvent;

public class example {
    public static void main(String[] args) {
        //get the current coordinates
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b  = a.getLocation();
        int xOrig = (int)b.getX();
        int yOrig = (int)b.getY();

        try {
            //simulate a click at 10, 50
            Robot r = new Robot();
            r.mouseMove(10, 50);
            r.mousePress(InputEvent.BUTTON1_MASK); //press the left mouse button
            r.mouseRelease(InputEvent.BUTTON1_MASK); //release the left mouse button

            //move the mouse back to the original position
            r.mouseMove(xOrig, yOrig);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

这篇关于如何在Cocoa中以编程方式模拟鼠标单击而不移动鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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