java 如何编写可以与其他程序交互的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7945444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:07:45  来源:igfitidea点击:

How to write a program that can interact with other programs

java

提问by Spart

I want to write a program that could play solitaire on windows. The user would run the program and open solitaire and watch as the cards move. Its easy to write the AI for the program that would play solitaire (I obviously don't have the source code for the Windows solitaire). One way I can think of doing this is if I took an image of the solitaire and analyzed it to determine the current state of the cards, then I could pass the current state of the cards to my program, which could determine the next move. But how would I execute the clicking command.

我想编写一个可以在 Windows 上玩纸牌的程序。用户将运行该程序并打开纸牌并观察纸牌的移动。为玩纸牌的程序编写 AI 很容易(我显然没有 Windows 纸牌的源代码)。我能想到的一种方法是,如果我拍摄纸牌的图像并对其进行分析以确定纸牌的当前状态,然后我可以将纸牌的当前状态传递给我的程序,它可以确定下一步行动。但是我将如何执行点击命令。

More generally, I want to write a program that can interact with another program like a user would. I have experience with C, C++, Java, Ruby. But I don't know how to even get started on this, or whether this can be even done?

更一般地说,我想编写一个可以像用户一样与另一个程序交互的程序。我有 C、C++、Java、Ruby 方面的经验。但我不知道如何开始,或者这是否可以完成?

采纳答案by LordDoskias

Java is not the language to do this. In order to facilitate functionality like this you need to rely heavily on windows API, especially those that allow you to inject input. As a starting point I can suggest you get acquainted with how windows application actually process the input. Thisis a good article on the subject.

Java 不是执行此操作的语言。为了促进这样的功能,您需要严重依赖 Windows API,尤其是那些允许您注入输入的 API。作为起点,我建议您熟悉 Windows 应用程序实际如何处理输入。是一篇关于这个主题的好文章。

回答by jaime

I'm not sure about all the other posts here, but Java has a Robot class that allows you to (easily) dispatch keyboard and mouse events to the underlying OS (windows in this case) that would allow you to do exactly what you're talking about. It's a little class I've used loads of times to "automate" tasks on windows.

我不确定这里的所有其他帖子,但 Java 有一个 Robot 类,它允许您(轻松)将键盘和鼠标事件分派到底层操作系统(在这种情况下为窗口),这将允许您完全按照您的要求进行操作重新谈论。这是一个小类,我用了很多次来“自动化”Windows 上的任务。

(I know this is an old thread, but in case anyone else out there in the world stumbles upon it like I have and want to do what you've outlined.)

(我知道这是一个旧线程,但以防万一世界上其他任何人像我一样偶然发现它并想做你所概述的事情。)

回答by Hovercraft Full Of Eels

If you want to automate Windows tasks, then use a language specifically written to perform this. Ones that come to mind include WinBatch, AutoIt, and AutoHotkey.

如果您想自动执行 Windows 任务,请使用专门编写的语言来执行此操作。想到的包括 WinBatch、AutoIt 和 AutoHotkey。

回答by Yahia

Windows has a so-called "UI Automation" API which is really helpful in implementing what you ask for - some links with details/source/samples:

Windows 有一个所谓的“UI 自动化”API,它对实现您的要求非常有帮助 - 一些带有详细信息/源代码/示例的链接:

REMARK:

评论:

Doing this from Java might be a bit hard since AFAIK you would need to use JNI for that.

从 Java 执行此操作可能有点困难,因为 AFAIK 您需要为此使用 JNI。

回答by Peer Sommerlund

Windows has a method SendInputwhich can be used to programmatially control mouse and keyboard. If you know which application you want to control this may be enough. However, you probably also want to examine what is on the screen. For this you need GetPixel.

Windows 有一个方法SendInput可用于以编程方式控制鼠标和键盘。如果您知道要控制哪个应用程序,这可能就足够了。但是,您可能还想检查屏幕上的内容。为此,您需要GetPixel

回答by crowne

I personally don't like screen scraper technologies very much, as they are often impacted by changes to the underlying application or even by changes in screen resolution.
However this seems to be an approach that you could adopt here.
I agree that java is not the best choice here.
Search for "screen scraper API" and you should find something to start you off.

我个人不太喜欢屏幕抓取技术,因为它们经常受到底层应用程序更改甚至屏幕分辨率更改的影响。
但是,这似乎是您可以在此处采用的方法。
我同意 java 不是这里的最佳选择。
搜索“screen scraper API”,你应该会找到一些东西让你开始。

Update:

更新:

The first hit that seems promising is http://community.screen-scraper.com/API
which counter-intuitively seems to be java based.

第一个看起来很有希望的点击是http://community.screen-scraper.com/API
,它与直觉相反似乎是基于 Java 的。