program for controlling mouse or keyboard

10,110

Solution 1

SendInput if you want to fake some mouse/keyboard events.

Solution 2

if you know how to program with python there is a module called pyautogui. to install this module fallow the steps below: if you are using windows and you have python installed. make sure that you are connected to the internet open CMD and type down: pip install pyautogui

Solution 3

You can use system.windows.forms to move the mouse and write keyboard input in C#. For example, the following code uses basic methods from system.windows.forms to move the mouse and write keyboard input

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);
    }
}
}

You will also need the System.Drawing to manipulate points with the mouse cursor, so don't forget it!

This is a wonderful video containing information relative to mouse and keyboard movement https://www.youtube.com/watch?v=48k9eyVsC-M

note: while these methods are wonderful for moving the mouse to my knowledge they cannot cause the mouse to click, so you will need to do research elsewhere for that information

Good Luck!

Share:
10,110
s5s
Author by

s5s

Updated on June 13, 2022

Comments

  • s5s
    s5s over 1 year

    I'm trying to learn how a program I write can control a GUI program that I'm running. I'm not sure the GUI program will necessarily expose an API for developers. Therefore, I think the best approach will be for my program to be able to exclusively control the keyboard and mouse temporarily or maybe share the control with the user (as long as the user isn't using the resources the program can control mouse/keyboard).

    I'm not sure how to do this. I know C/C++, Python and Java. I'm mainly looking to implement this on Windows (I know C#).

    I'm not exactly sure what I'm looking for (in terms of keywords) so I don't know how to google for information. Any help would be appreciated.

    EDIT: I thought I'd mention that I'm just looking for a method for my code to control mouse and keyboard. The inputs to this code will come from another piece of code which (hopefully) knows what the mouse/keyboard will have to do. Currently I just want to learn how to make my code control mouse/keyboard using a predefined control commands (e.g. move to position (100,100), click, type "abcd" etc.

    • ThiefMaster
      ThiefMaster over 11 years
      You do not want to control the mouse (cursor) or keyboard but rather send messages such as WM_CLICK to the correct window (or more correct, the correct element inside the correct window).
    • chris
      chris over 11 years