Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to create key presser for game it require to press (Space + number) to do some action i tried this code

C#
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



C#
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:

C#
//this code problem is repeat all the keys
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);
        }


C#
//this code problem is not release the space
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);
        }


C#
//this code problem is not release the space
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);
        }


C#
//this code problem is it do nothing
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);
        }


C#
//this code problem is it do nothing
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);
        }
Posted
Updated 5-Feb-18 7:19am

See the remark at the top of the documentation for keybd_event function (Windows)[^].
 
Share this answer
 
Comments
Member 7912784 3-Feb-18 8:28am    
if u mean {Note This function has been superseded. Use SendInput instead.}

i try SendInput and the same error the application keep repeat the keys
Richard MacCutchan 3-Feb-18 8:46am    
The values that you are sending to keybd_event are wrong. Look at the documentation more closely.
Member 7912784 3-Feb-18 9:40am    
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x101;
const int KeyDown = 0x0001;
const int KeyUp = 0x0002;
const byte VK_SPACE = 0x20;
const byte SpaceSC = 39;


private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event(VK_SPACE, SpaceSC, 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(VK_SPACE, SpaceSC, KeyUp, 0);
}

code u please point to the wrong values in this code because it keep repeat the keys
Richard MacCutchan 3-Feb-18 11:14am    
You are sending an invalid value for KeyDown - please read the documentation again.
Member 7912784 3-Feb-18 15:14pm    
const uint Key_Down = 0x0001;
const uint Key_Up = 0x0002;
const byte VK_SPACE = 0x20;
const byte SpaceSC = 39;




private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event(VK_SPACE, 0x45, Key_Down | 0, 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(VK_SPACE, 0x45, Key_Down| Key_Up, 0);
}


--------------------------------------------------------------

const uint Key_Down = 0x0001;
const uint Key_Up = 0x0002;
const byte VK_SPACE = 0x20;
const byte SpaceSC = 39;




private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event(VK_SPACE, 0, 0, 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(VK_SPACE, 0, Key_Up, 0);
}

-------------------------------------------------------------

const uint Key_Down = 0x0001;
const uint Key_Up = 0x0002;
const byte VK_SPACE = 0x20;
const byte SpaceSC = 39;




private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event(VK_SPACE, 0, Key_Down , 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(VK_SPACE, 0, Key_Up, 0);
}

----------------------------------------------------------------

i try all that and more but same error and msdn say the the key down value is
KEYEVENTF_EXTENDEDKEY
0x0001
as my Key_Down i didn't get the error in my code why it keep repeat all the pressing
i find way to solve it

C#
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace SendKeysWithModefire
{
   public class KeysTest
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, uint 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;
        const uint Key_Down = 0x0001;
        const uint Key_Up = 0x0002;
        private volatile bool _shouldStop;
        private Process _sroProcess;
        private Keys _modefirKey;
        private Keys _pressKey;

        public KeysTest(Process targetSro,Keys modKey,Keys pressKey)
        {
            _sroProcess = targetSro;
            _modefirKey = modKey;
            _pressKey = pressKey;
        }

        public void SendKeys()
        {
            SendKeyWithModefir(_sroProcess, (byte)_modefirKey, (byte)_pressKey);
        }

        public void SendKeyWithModefir(Process _p, byte ModKey, byte PressKey)
        {
            while (!_shouldStop)
            {
                keybd_event(ModKey, 0, Key_Down, 0);
                Thread.Sleep(50);
                PostMessage(_p.MainWindowHandle, WM_KEYDOWN, (IntPtr)(PressKey), (IntPtr)(0));
                PostMessage(_p.MainWindowHandle, WM_KEYUP, (IntPtr)(PressKey), (IntPtr)(0));
                Thread.Sleep(50);
                keybd_event(ModKey, 0, Key_Up, 0);
            }
        }

        public void RequestStop()
        {
            _shouldStop = true;
        }

        ~KeysTest()
        {
            GC.Collect();
        }
    }
}


and that's the call for it

C#
private KeysTest sendingKeys;
private Thread startSending;

...............

//that code send Left Control + 1
//s is the target process
sendingKeys = new KeysTest(s, Keys.LControlKey, Keys.D1);
startSending = new Thread(sendingKeys.SendKeys); 
startSending.Start();
while (!startSending.IsAlive) ;
Thread.Sleep(2000);
sendingKeys.RequestStop();
startSending.Join();
startSending.Abort();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900