如何使用鼠标指针和键盘快捷键捕获文本? [英] How To Capture Text Using Mouse Pointer And Keyboard Shortcuts?

查看:91
本文介绍了如何使用鼠标指针和键盘快捷键捕获文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#或Java使用鼠标指针和键盘快捷键从打开的窗口捕获文本 (例如巴比伦),因此 我需要知道什么以及如何实现?

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 ?

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

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

推荐答案

使用脚本语言创建您想做的事情的草稿.

您可以使用 AutoHotKey AutoIt 之类的程序.请注意,您的自动录音机包括在内,可以为您提供基本的草稿.您可以将这些脚本编译为可执行文件,然后使用Shell Execute( c#; java (exec))或作为新进程运行( java (流程构建器)).最好是后者.

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.

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

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

使用Java.

Java优雅地包含 Robot 类,即可执行此操作.

Using Java.

Java contains Robot class, to do this.

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

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.

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

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.

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

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.

使用Robot进行以下操作的应用程序 除自测以外的目的 应该处理这些错误情况 优雅地.

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.
    }
}

java2s上的更多机器人示例:(链接)

More Robot examples at java2s: ( link )

  1. 机器人:createScreenCapture(矩形screenRect)
  2. 机器人:getPixelColor(int x,int y)
  3. 机器人:keyPress(int键码)
  4. 机器人:keyRelease(int键码)
  5. 机器人:mouseMove(int x,int y)
  6. 机器人:mousePress(int按钮)
  7. 机器人:mouseRelease(int按钮)
  8. 机器人:mouseWheel(int wheelAmt)

使用C#.

多种解决方案.只需 google "

Using C#.

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

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

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

您可以使用Windows API,但这需要一些乏味的工作.您不想这样做,但实际上您不想这么做,但是如果您这样做,那么一定要检查一下:

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:

  • http://www.pinvoke.net/default.aspx/user32.mouse_event
  • http://www.pinvoke.net/default.aspx/user32.sendinput

我建议您使用 inputsimulator .示例:

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.          
        }
    }
}

这篇关于如何使用鼠标指针和键盘快捷键捕获文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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