【问题标题】:program for controlling mouse or keyboard控制鼠标或键盘的程序
【发布时间】:2012-05-09 12:08:20
【问题描述】:

我正在尝试了解我编写的程序如何控制我正在运行的 GUI 程序。我不确定 GUI 程序是否一定会为开发人员公开 API。因此,我认为最好的方法是让我的程序能够暂时专门控制键盘和鼠标,或者可能与用户共享控件(只要用户不使用程序可以控制鼠标/键盘的资源)。

我不知道该怎么做。我知道 C/C++、Python 和 Java。我主要是想在 Windows 上实现这个(我知道 C#)。

我不确定我在寻找什么(就关键字而言),所以我不知道如何在谷歌上搜索信息。任何帮助将不胜感激。

编辑:我想我会提到我只是在为我的代码寻找一种方法来控制鼠标和键盘。该代码的输入将来自另一段代码,该代码(希望)知道鼠标/键盘必须做什么。目前我只想学习如何使用预定义的控制命令(例如移动到位置 (100,100)、单击、键入“abcd”等)使我的代码控制鼠标/键盘。

【问题讨论】:

标签: c++ c windows keyboard mouse


【解决方案1】:

如果您知道如何使用 python 编程,那么有一个名为 pyautogui 的模块。 按照以下步骤安装此模块: 如果您使用的是 windows 并且安装了 python。 确保您已连接到互联网 打开CMD并输入:pip install pyautogui

【讨论】:

    【解决方案2】:

    您可以使用 system.windows.forms 在 C# 中移动鼠标和编写键盘输入。 例如,下面的代码使用 system.windows.forms 中的基本方法来移动鼠标和编写键盘输入

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    //Make sure to add these as references in your project ahead of time
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace ConsoleApplication14
    {
    class Program
    {
        static void Main(string[] args)
        {
            //The following code sets the cursor position to the point Xpos,Ypos 
    //on the screen
            int Xpos = 0;
            int Ypos = 0;
            Cursor.Position = new Point(Xpos,Ypos);
    
            //The following writes the string KeyData to keyboard input
            string KeyData = "Hello World";
            SendKeys.SendWait(KeyData);
        }
    }
    }
    

    您还需要 System.Drawing 来使用鼠标光标操作点,所以不要忘记它!

    这是一个精彩的视频,包含与鼠标和键盘移动相关的信息https://www.youtube.com/watch?v=48k9eyVsC-M

    注意:据我所知,虽然这些方法非常适合移动鼠标,但它们不会导致鼠标点击,因此您需要在其他地方进行研究以获取该信息

    祝你好运!

    【讨论】:

      【解决方案3】:

      SendInput 如果你想伪造一些鼠标/键盘事件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 2010-12-29
        • 2016-01-16
        • 2014-06-26
        相关资源
        最近更新 更多