I try to create key presser for game it require to press (Space + number) to do some action i tried this code
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x101;
public Form1()
{
InitializeComponent();
LoadProcess();
}
private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event((byte)Keys.Space, 0, 1, 0);
Thread.Sleep(50);
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.D1, IntPtr.Zero);
PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.D1, IntPtr.Zero);
keybd_event((byte)Keys.Space, 0, 0, 0);
}
private void LoadProcess()
{
comboBox1.Items.Clear();
comboBox1.DisplayMember = "MainWindowTitle";
foreach (Process item in Process.GetProcesses())
{
try
{
if (!string.IsNullOrEmpty(item.MainWindowTitle))
comboBox1.Items.Add(item);
}
catch { }
}
}
}
}
it work but keep pressing space
when i try to release it by this command
keybd_event((byte)Keys.Space, 0, 2, 0);
the application keep repeat pressing all the keys
and if didn't add this line the space didn't reals
how to fix this?
What I have tried:
const int KeyDown = 0x0001;
const int KeyUp = 0x0002;
.......
private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event((byte)Keys.Space, 0, KeyDown, 0);
Thread.Sleep(50);
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.D1, IntPtr.Zero);
PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.D1, IntPtr.Zero);
keybd_event((byte)Keys.Space, 0, KeyUp, 0);
}
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x101;
.........
private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event((byte)Keys.Space, 0, (int)WM_KEYDOWN, 0);
Thread.Sleep(50);
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.D1, IntPtr.Zero);
PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.D1, IntPtr.Zero);
keybd_event((byte)Keys.Space, 0, (int)WM_KEYUP, 0);
}
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x101;
.........
private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event((byte)Keys.Space, 0, (int)WM_KEYDOWN, 0);
Thread.Sleep(50);
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.D1, IntPtr.Zero);
PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.D1, IntPtr.Zero);
keybd_event((byte)Keys.Space, 0, 0, 0);
}
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x101;
.........
private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event((byte)Keys.Space, 0, 1, 0);
Thread.Sleep(50);
keybd_event((byte)Keys.D1, 0, 1, 0);
keybd_event((byte)Keys.D1, 0, 2, 0);
keybd_event((byte)Keys.Space, 0, 2, 0);
}
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x101;
const int KeyDown = 0x0001;
const int KeyUp = 0x0002;
.........
private void button1_Click(object sender, EventArgs e)
{
keybd_event((byte)Keys.Space, 0, KeyDown, 0);
Thread.Sleep(50);
keybd_event((byte)Keys.D1, 0, KeyDown, 0);
Thread.Sleep(50);
keybd_event((byte)Keys.D1, 0, KeyUp, 0);
keybd_event((byte)Keys.Space, 0, KeyUp, 0);
}