如何使用鼠标指针和键盘快捷键捕获文本?

发布于 2024-09-29 03:14:27 字数 159 浏览 3 评论 0原文

我想使用 C# 或 java 使用鼠标指针和键盘快捷键从打开的窗口捕获文本 (如 babylon ),所以 我需要知道什么以及如何实施?

我需要使用哪些库?或者我可以使用 winapi 吗?

i want to capture text from opened windows using mouse pointer and keyboard shortcut using C# or java
( like babylon ) , so
what i need to know and how to implement it ?

what are the libraries i need to use ? or can i use winapi ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-10-06 03:14:27

使用脚本语言创建您想要执行的操作的草稿。

您可以使用AutoHotKeyAutoIt等程序。请注意,您包含自动录音机,它可以为您提供基本草稿。您可以将这些脚本编译为可执行文件,并使用 Shell Execute 从 C# 或 Java 调用它们 ( c#; java< /a> (exec) ) 或作为新进程运行 ( c#; java(流程构建器))。后者是优选的。

下面是一个示例,说明如何将“暂停”键映射到从屏幕上选择文本的功能,然后使用 AutoHotKey 将其复制并粘贴到另一个位置。 Shift + left click 用于背景选择所有文本。请注意,这是最简单的示例,不会通过其指针调用窗口并使用固定位置(并且仅适用于一种分辨率)。

HotKeySet("{PAUSE}", "getInput")

While 1
    Sleep(100)
Wend


Func getInput()
    MouseClick("left",272,241,1)
    Sleep(100)
    MouseClick("left",272,241,1)
    Send("{SHIFTDOWN}")
    MouseClick("left",272,241,1)
    MouseClick("left",529,242,2)
    Send("{SHIFTUP}{CTRLDOWN}c{CTRLUP}")
    MouseClick("left",656,42,1)
    Sleep(100)  
    MouseClick("left",696,42,1)
    Send("{CTRLDOWN}a")
    Send("{DELETE}")
    Send("{CTRLDOWN}v{CTRLUP}")
    MouseClick("left",1178,44,1)
EndFunc

使用Java。

Java 包含 Robot 类来执行此操作。

该类用于生成native
系统输入事件的目的
测试自动化、自运行
演示和其他应用程序
鼠标和键盘的控制是
需要。机器人的主要目的
是为了促进自动化测试
Java 平台实现。

使用类生成输入
事件不同于将事件发布到
AWT 事件队列或 AWT 组件
因为事件是在中生成的
平台的本机输入队列。为了
例如,Robot.mouseMove 实际上会
移动鼠标光标而不仅仅是
生成鼠标移动事件。

请注意,某些平台需要
特殊特权或扩展
访问低级输入控制。如果
当前平台配置确实
不允许输入控制,
AWTException 将在以下情况抛出:
尝试构造机器人对象。为了
例如,X-Window 系统会抛出
如果 XTEST 2.2 则例外
不支持标准扩展
(或未启用)由 X 服务器。

使用机器人的应用程序
自测试以外的目的
应该处理这些错误情况
优雅地。

您可以自行定制如何使用 Robot,但一般方式:

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class Tester {
    public static void doLeftMouseClick(Robot r, int x, int y) {
        r.mouseMove(x, y);
        r.mousePress(InputEvent.BUTTON1_MASK);
        r.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    public static void doLeftMouseClickEvent(Robot r, int x, int y, int nr) {
        r.mouseMove(x, y);
        if (nr == 1)
            r.mousePress(InputEvent.BUTTON1_MASK);
        else
            r.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    public static void main(String args[]) throws Exception {
        Robot r = new Robot();
        doLeftMouseClick(r, 272, 241);
        r.delay(1000);
        doLeftMouseClick(r, 272, 241);
        r.keyPress(KeyEvent.SHIFT_MASK);
        doLeftMouseClickEvent(r, 272, 241, 1);
        doLeftMouseClickEvent(r, 529, 242, 2);
        r.keyRelease(KeyEvent.SHIFT_MASK);
        r.keyPress(KeyEvent.CTRL_MASK);
        r.keyPress(KeyEvent.VK_C);
        r.keyRelease(KeyEvent.CTRL_MASK);
        // etc.
    }
}

java2s 上的更多 Robot 示例:( 链接

  1. 机器人:createScreenCapture(矩形 screenRect)
  2. 机器人:getPixelColor(int x,int y)
  3. 机器人:keyPress(int keycode)
  4. 机器人:keyRelease(int keycode)
  5. 机器人:mouseMove(int x,int y)
  6. 机器人:mousePress(intbuttons)
  7. 机器人:mouseRelease(intbuttons)
  8. 机器人:mouseWheel(intwheelAmt)

使用 C#。

无数解决方案。只需google测试自动化 c#" 或 "间谍 c#"。

MSDN:SendKeys
MSDN:如何:在代码中模拟鼠标和键盘事件

您可以使用windows API,但是需要一些繁琐的工作。你不想这样做,你真的不想这样做,但如果你这样做,那么一定要检查一下:

我建议您使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add reference to following
using WindowsInput;
using System.Windows.Forms;
using System.Drawing;

namespace ConsoleApplicationTester
{
    class Program
    {
        public static void doLeftMouseClick(int x, int y)
        {
            Cursor.Position = new System.Drawing.Point(x, y);
            InputSimulator.SimulateKeyPress(VirtualKeyCode.LBUTTON);
        }
        public static void doLeftMouseClickEvent(int x, int y, int nr)
        {
            Cursor.Position = new Point(x, y);
            if(nr==1)
                InputSimulator.SimulateKeyDown(VirtualKeyCode.LBUTTON);
            else
                InputSimulator.SimulateKeyUp(VirtualKeyCode.LBUTTON);
        }

        static void Main(string[] args){
            doLeftMouseClick( 272, 241);
            System.Threading.Thread.Sleep(100);
            doLeftMouseClick( 272, 241);
            InputSimulator.SimulateKeyDown(VirtualKeyCode.MENU);
            doLeftMouseClickEvent(272, 241, 1);
            doLeftMouseClickEvent(529, 242, 2);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.MENU);
            InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_C);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
            // etc.          
        }
    }
}

Use a scripting language to create a draft of what you want to do.

You can use programs like AutoHotKey or AutoIt. Note, that thy include auto recorder, that gives you a basic draft. You can compile those scripts to executables, and call them from C# or Java using Shell Execute ( c#; java (exec) ) or run as new Process ( c#; java (process builder) ). Latter is preferred.

Here is an example of how to map a key 'pause', to a function that selects a text from screen, copy's it and pastes it in another place using AutoHotKey. Shift + left click is used on background to select all the text. Note, that this is simplest example and does not invoke window by its pointer and uses fixed locations (and work only for one resolution).

HotKeySet("{PAUSE}", "getInput")

While 1
    Sleep(100)
Wend


Func getInput()
    MouseClick("left",272,241,1)
    Sleep(100)
    MouseClick("left",272,241,1)
    Send("{SHIFTDOWN}")
    MouseClick("left",272,241,1)
    MouseClick("left",529,242,2)
    Send("{SHIFTUP}{CTRLDOWN}c{CTRLUP}")
    MouseClick("left",656,42,1)
    Sleep(100)  
    MouseClick("left",696,42,1)
    Send("{CTRLDOWN}a")
    Send("{DELETE}")
    Send("{CTRLDOWN}v{CTRLUP}")
    MouseClick("left",1178,44,1)
EndFunc

Using Java.

Java contains Robot class, to do this.

This class is used to generate native
system input events for the purposes
of test automation, self-running
demos, and other applications where
control of the mouse and keyboard is
needed. The primary purpose of Robot
is to facilitate automated testing of
Java platform implementations.

Using the class to generate input
events differs from posting events to
the AWT event queue or AWT components
in that the events are generated in
the platform's native input queue. For
example, Robot.mouseMove will actually
move the mouse cursor instead of just
generating mouse move events.

Note that some platforms require
special privileges or extensions to
access low-level input control. If the
current platform configuration does
not allow input control, an
AWTException will be thrown when
trying to construct Robot objects. For
example, X-Window systems will throw
the exception if the XTEST 2.2
standard extension is not supported
(or not enabled) by the X server.

Applications that use Robot for
purposes other than self-testing
should handle these error conditions
gracefully.

You can tailor how you use Robot yourself, but general way:

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class Tester {
    public static void doLeftMouseClick(Robot r, int x, int y) {
        r.mouseMove(x, y);
        r.mousePress(InputEvent.BUTTON1_MASK);
        r.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    public static void doLeftMouseClickEvent(Robot r, int x, int y, int nr) {
        r.mouseMove(x, y);
        if (nr == 1)
            r.mousePress(InputEvent.BUTTON1_MASK);
        else
            r.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    public static void main(String args[]) throws Exception {
        Robot r = new Robot();
        doLeftMouseClick(r, 272, 241);
        r.delay(1000);
        doLeftMouseClick(r, 272, 241);
        r.keyPress(KeyEvent.SHIFT_MASK);
        doLeftMouseClickEvent(r, 272, 241, 1);
        doLeftMouseClickEvent(r, 529, 242, 2);
        r.keyRelease(KeyEvent.SHIFT_MASK);
        r.keyPress(KeyEvent.CTRL_MASK);
        r.keyPress(KeyEvent.VK_C);
        r.keyRelease(KeyEvent.CTRL_MASK);
        // etc.
    }
}

More Robot examples at java2s: ( link )

  1. Robot: createScreenCapture(Rectangle screenRect)
  2. Robot: getPixelColor(int x, int y)
  3. Robot: keyPress(int keycode)
  4. Robot: keyRelease(int keycode)
  5. Robot: mouseMove(int x, int y)
  6. Robot: mousePress(int buttons)
  7. Robot: mouseRelease(int buttons)
  8. Robot: mouseWheel(int wheelAmt)

Using C#.

There are myriad of solutions. Just google "Test Automation c#" or "spy c#".

MSDN: SendKeys
MSDN: How to: Simulate Mouse and Keyboard Events in Code

You can use windows API, but it requires some tedious work. You don't want to do that, you really don't, but if you do, then definitely check out:

I recommend you use inputsimulator. Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add reference to following
using WindowsInput;
using System.Windows.Forms;
using System.Drawing;

namespace ConsoleApplicationTester
{
    class Program
    {
        public static void doLeftMouseClick(int x, int y)
        {
            Cursor.Position = new System.Drawing.Point(x, y);
            InputSimulator.SimulateKeyPress(VirtualKeyCode.LBUTTON);
        }
        public static void doLeftMouseClickEvent(int x, int y, int nr)
        {
            Cursor.Position = new Point(x, y);
            if(nr==1)
                InputSimulator.SimulateKeyDown(VirtualKeyCode.LBUTTON);
            else
                InputSimulator.SimulateKeyUp(VirtualKeyCode.LBUTTON);
        }

        static void Main(string[] args){
            doLeftMouseClick( 272, 241);
            System.Threading.Thread.Sleep(100);
            doLeftMouseClick( 272, 241);
            InputSimulator.SimulateKeyDown(VirtualKeyCode.MENU);
            doLeftMouseClickEvent(272, 241, 1);
            doLeftMouseClickEvent(529, 242, 2);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.MENU);
            InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_C);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
            // etc.          
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文