[C#] Mouse & KeyBoard Events

gooby pls
Decennium
Joined
Mar 22, 2008
Messages
830
Reaction score
177
Hello, I have been working in a program the last 30 minutes, and I can't seem to successfully make the program press a key or mouse button.
I need the program to execute the events when a key is pressed, for example, When I press F1, start pressing letter "Q" without stopping until the same key is pressed again.



EDIT 1:
PHP:
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.F1)
            {
                //Event to repeatedly press letter Q
            }
            else
            {
                // Do nothing
            }

        }

Please help me fill out the blanks inside the curly braces.




EDIT 2: Program has GUI...


Regards,
Alan Alvarez
 
Last edited:
Yea, its a bot looking program. :P
Ill send you the source, but I haven't finished this yet...

Stupid events... >.<
 
I don't know how you did it but this is pretty much how i did it.
Code:
 [return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(uint Msg,int wParam,int lParam);
//Define WM_KEYDOWN
const int WM_KEYDOWN = 0x100;

and then simply use the PostMessage method i.e.
Code:
PostMessage(WM_KEYDOWN, 0x41, 0x1e0001); // A press

Here is a list of key presses:http://pastebin.com/f6c1db818

If you want it to latch on to a program use:
Code:
IntPtr hWnd = FindWindow(null,"MyProcessName");
 
I don't know how you did it but this is pretty much how i did it.
Code:
 [return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(uint Msg,int wParam,int lParam);
//Define WM_KEYDOWN
const int WM_KEYDOWN = 0x100;
and then simply use the PostMessage method i.e.
Code:
PostMessage(WM_KEYDOWN, 0x41, 0x1e0001); // A press
Here is a list of key presses:http://pastebin.com/f6c1db818

If you want it to latch on to a program use:
Code:
IntPtr hWnd = FindWindow(null,"MyProcessName");


Very useful! You can leave the game, for my case, minimized and do something else while your character is still killing monsters :)

ty man!










PD: Still open for different events that might be useful!
 
Ah if you want to do the "latch on" thing here the postmessage method:

Code:
static extern bool PostMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);


I am having a problem applying this...
Would you be able to help me?
 
I have found this...

PHP:
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.F1)
            {
                //Event to repeatedly press letter Q
            }
            else
            {
                // Do nothing
            }

        }

Please help me fill out the blanks inside the curly braces.


Regards,
Alan Alvarez
 
I am having a problem applying this...
Would you be able to help me?

Oh, if you were looking for the full version, here it is:
Code:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);

If that isn't a problem then please post it here.
 
Oh, if you were looking for the full version, here it is:
Code:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);
If that isn't a problem then please post it here.



This is the problem I have:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (
e.KeyChar == (char)Keys.F1)
{
//Event to repeatedly press letter Q
}
else
{
// Do nothing
}

}




I cant seem to find a good way to execute the event that presses Q non stoping... I also need it to stop when F1 is pressed again.
 
Key to toggle a true/false variable, use a do...while var = true loop to execute the code.

You'll need to use a different method for the hotkeys, though. Your current method will only work when the form has window focus. Google a guide for global hotkeys in C# to find out how to do it properly.
 
Last edited:
Thanks Ron, but I still can't find a way to do it... :S

Would you or someone mind writing it here?
Thanks!
 
Any Help?

---------- Post added at 07:17 PM ---------- Previous post was at 06:57 PM ----------

Im getting this error: The name 'FindWindow' does not exist in the current context

while using this:
PHP:
IntPtr hWnd = FindWindow(null, "MyProcessName");
 
OK now im using

PHP:
        private void autoHPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

            Process[] processes = Process.GetProcessesByName("notepad");

            foreach (Process p in processes)

            {

             SendKeys.Send("{Q}");

            }
        }

And works great, just need to make it send the key without stop until I press F1 again :)
 
PHP:
        private void autoHPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

            Process[] processes = Process.GetProcessesByName("notepad");

            foreach (Process p in processes)

            {
             do
             {

             SendKeys.Send("{Q}");
             }
            while (  ) // keypress code 

            }
        }


that's what I would do
 
PHP:
        private void autoHPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

            Process[] processes = Process.GetProcessesByName("notepad");

            foreach (Process p in processes)

            {
             do
             {

             SendKeys.Send("{Q}");
             }
            while (  ) // keypress code 

            }
        }
that's what I would do

I get an error on "while ( )" even when adding ";"
 
Back