使用Perl,Python或Ruby,如何编写程序以在预定时间“点击”屏幕?

时间:2022-09-17 12:16:48

Using Perl, Python, or Ruby, can I write a program, probably calling Win32 API, to "click" on the screen at scheduled time, like every 1 hour?

使用Perl,Python或Ruby,我可以编写一个程序,可能调用Win32 API,在预定的时间点击屏幕,就像每1小时一样?

Details:

This is for experimentation -- and can the clicking be effective on Flash content as well as any element on screen? It can be nice if the program can record where on screen the click needs to happen, or at least draw a red dot on the screen to show where it is clicking on.

这是为了实验 - 点击对Flash内容以及屏幕上的任何元素都有效吗?如果程序可以记录点击需要在屏幕上显示的位置,或者至少在屏幕上绘制一个红点以显示点击的位置,那就太好了。

Can the click be targeted towards a window or is it only a general pixel on the screen? What if some virus scanning program pops up covering up the place where the click should happen? (although if the program clicks on the white space of a window first, then it can bring that window to the foreground first).

点击是针对窗口还是只是屏幕上的一般像素?如果弹出一些病毒扫描程序覆盖点击发生的地方怎么办? (尽管如果程序首先点击窗口的空白区域,那么它可以首先将该窗口带到前台)。

By the way, can Grease Monkey or any Firefox add-on be used to do this too?

顺便说一句,Grease Monkey或任何Firefox附加组件也可以用来做这个吗?

5 个解决方案

#1


If you are trying to automate some task in a website you might want to look at WWW::Selenium. It, along with Selenium Remote Control, allows you to remote control a web browser.

如果您尝试在网站中自动执行某项任务,您可能需要查看WWW :: Selenium。它与Selenium Remote Control一起允许您远程控制Web浏览器。

#2


In Python there is ctypes and in Perl there is Win32::API

在Python中有ctypes,在Perl中有Win32 :: API

ctypes Example

from ctypes import *
windll.user32.MessageBoxA(None, "Hey MessageBox", "ctypes", 0);

Win32::Api Example

use Win32::GUI qw( WM_CLOSE );
my $tray = Win32::GUI::FindWindow("WindowISearchFor","WindowISearchFor");
Win32::GUI::SendMessage($tray,WM_CLOSE,0,0);

#3


To answer the actual question, in Perl, you would use the SendMouse (and the associated functions) provided by the Win32::GuiTest module.

要回答实际问题,在Perl中,您将使用Win32 :: GuiTest模块提供的SendMouse(和相关函数)。

#!/usr/bin/perl

use strict;
use warnings;

use Win32::GuiTest qw( MouseMoveAbsPix SendMouse );

MouseMoveAbsPix(640,400);
SendMouse "{LEFTCLICK}";

__END__

UPDATE:

What if some virus scanning program pops up covering up the place where the click should happen?

如果弹出一些病毒扫描程序覆盖点击发生的地方怎么办?

In that case, you would use FindWindowLike to find the window and MouseClick to send a click to that specific window.

在这种情况下,您将使用FindWindowLike查找窗口,使用MouseClick将点击发送到该特定窗口。

#4


If using a different tool is allowed, you should take a look at AutoHotkey or AutoIt. These tools were made for this sort of thing, and I've always been keen on using the right tools for the right jobs.

如果允许使用其他工具,则应该查看AutoHotkey或AutoIt。这些工具是为这类工具而制造的,我一直热衷于使用合适的工具来完成正确的工作。

AutoHotkey is based on AutoIt I believe, and it is my personal preference. You only really need 2 functions for what you're trying to achieve, MouseMove and MouseClick.

AutoHotkey基于AutoIt我相信,这是我个人的偏好。你只需要2个函数来实现你想要实现的功能,MouseMove和MouseClick。

#5


I find this is easier to approach in Java or C++. Java has a Robot class that allows you to just pass x, y coordinates and click somewhere. Using C++, you can achieve that same functionality using mouse_event() or SendMessage() with the WM_MOUSE_DOWN flag. SendMessage is more technical but it allows you to use FindWindow() and send mouse clicks to a specific window, even if it's minimized.

我发现这在Java或C ++中更容易实现。 Java有一个Robot类,它允许你只传递x,y坐标并点击某处。使用C ++,您可以使用带有WM_MOUSE_DOWN标志的mouse_event()或SendMessage()来实现相同的功能。 SendMessage更具技术性,但它允许您使用FindWindow()并将鼠标单击发送到特定窗口,即使它已被最小化。

Using a scripting language like Python or Ruby, I'd guess that you'd end up hooking into one of these Windows API functions anyway.

使用像Python或Ruby这样的脚本语言,我猜你最终会陷入其中一个Windows API函数。

#1


If you are trying to automate some task in a website you might want to look at WWW::Selenium. It, along with Selenium Remote Control, allows you to remote control a web browser.

如果您尝试在网站中自动执行某项任务,您可能需要查看WWW :: Selenium。它与Selenium Remote Control一起允许您远程控制Web浏览器。

#2


In Python there is ctypes and in Perl there is Win32::API

在Python中有ctypes,在Perl中有Win32 :: API

ctypes Example

from ctypes import *
windll.user32.MessageBoxA(None, "Hey MessageBox", "ctypes", 0);

Win32::Api Example

use Win32::GUI qw( WM_CLOSE );
my $tray = Win32::GUI::FindWindow("WindowISearchFor","WindowISearchFor");
Win32::GUI::SendMessage($tray,WM_CLOSE,0,0);

#3


To answer the actual question, in Perl, you would use the SendMouse (and the associated functions) provided by the Win32::GuiTest module.

要回答实际问题,在Perl中,您将使用Win32 :: GuiTest模块提供的SendMouse(和相关函数)。

#!/usr/bin/perl

use strict;
use warnings;

use Win32::GuiTest qw( MouseMoveAbsPix SendMouse );

MouseMoveAbsPix(640,400);
SendMouse "{LEFTCLICK}";

__END__

UPDATE:

What if some virus scanning program pops up covering up the place where the click should happen?

如果弹出一些病毒扫描程序覆盖点击发生的地方怎么办?

In that case, you would use FindWindowLike to find the window and MouseClick to send a click to that specific window.

在这种情况下,您将使用FindWindowLike查找窗口,使用MouseClick将点击发送到该特定窗口。

#4


If using a different tool is allowed, you should take a look at AutoHotkey or AutoIt. These tools were made for this sort of thing, and I've always been keen on using the right tools for the right jobs.

如果允许使用其他工具,则应该查看AutoHotkey或AutoIt。这些工具是为这类工具而制造的,我一直热衷于使用合适的工具来完成正确的工作。

AutoHotkey is based on AutoIt I believe, and it is my personal preference. You only really need 2 functions for what you're trying to achieve, MouseMove and MouseClick.

AutoHotkey基于AutoIt我相信,这是我个人的偏好。你只需要2个函数来实现你想要实现的功能,MouseMove和MouseClick。

#5


I find this is easier to approach in Java or C++. Java has a Robot class that allows you to just pass x, y coordinates and click somewhere. Using C++, you can achieve that same functionality using mouse_event() or SendMessage() with the WM_MOUSE_DOWN flag. SendMessage is more technical but it allows you to use FindWindow() and send mouse clicks to a specific window, even if it's minimized.

我发现这在Java或C ++中更容易实现。 Java有一个Robot类,它允许你只传递x,y坐标并点击某处。使用C ++,您可以使用带有WM_MOUSE_DOWN标志的mouse_event()或SendMessage()来实现相同的功能。 SendMessage更具技术性,但它允许您使用FindWindow()并将鼠标单击发送到特定窗口,即使它已被最小化。

Using a scripting language like Python or Ruby, I'd guess that you'd end up hooking into one of these Windows API functions anyway.

使用像Python或Ruby这样的脚本语言,我猜你最终会陷入其中一个Windows API函数。