C#C
C#3y ago
blendet

simulate key presses on a specific application

I am trying to create a form app that lets you chose the process and key which you want to click on the specific process.

I am unsure on how to simulate the key press.

Here is my code:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace keyclick
{
    public partial class Form1 : Form
    {

        // Global variables
        private string key;
        private string process;

        public Form1()
        {
            InitializeComponent();
        }

        // Get processes and add it to the combo box.
        private void processList_Dropdown(object sender, EventArgs e)
        {
            Process[] processAll = Process.GetProcesses();

            foreach (Process process in processAll)
            {
                processList.Items.Add(process.ProcessName);
            }
        }

        // Add all keys into combo box.
        private void Form1_Load(object sender, EventArgs e)
        {
            Array keyValues = Enum.GetValues(typeof(Keys));

            foreach (var key in keyValues)
            {
                string keyName = key.ToString();
                keyList.Items.Add(keyName);
            }
        }

        // Assigning the user's chosen key to a global variable.
        private void keyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            key = keyList.SelectedItem.ToString();
        }

        // Assigning the user's chosen key to a global variable.
        private void processList_SelectedIndexChanged(object sender, EventArgs e)
        {
            process = processList.SelectedItem.ToString();
            MessageBox.Show(process);
        }
    }
}

Any help will be appreciated!
Was this page helpful?