| View previous topic :: View next topic |
| Author |
Message |
darckalan Newbie cheater
Reputation: 0
Joined: 27 Sep 2009 Posts: 14
|
Posted: Sun Sep 27, 2009 8:02 pm Post subject: [C#] SendKeys unfocused |
|
|
OK, I just want to know how to SendKeys to a specific application.
here is my code
| Code: | private void autoHPToolStripMenuItem_Click(object sender, EventArgs e)
{
IntPtr hWnd = FindWindow(null, "MyProcessName");
SendKeys.Send("{Q}");
} |
I want to know what im doing wrong... And how to make it stop when F1 key is pressed.
Please try to post the fixed code, since I have a hard time looking trough tons of pages...
Regards,
Alan Alvarez |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Sep 27, 2009 9:31 pm Post subject: |
|
|
It only sends keys to the active window, which should be obvious if you read the first couple hits on google.
| Quote: | | Because there is no managed method to activate another application, you can either use this class within the current application or use native Windows methods, such as FindWindow and SetForegroundWindow, to force focus on other applications. |
As for making it stop, just use a timer or spawn a thread that runs a loop until you press a certain key. |
|
| Back to top |
|
 |
darckalan Newbie cheater
Reputation: 0
Joined: 27 Sep 2009 Posts: 14
|
Posted: Sun Sep 27, 2009 10:37 pm Post subject: |
|
|
Ok, I got this...
| Code: | 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
}
} |
I want to know what to put in "while ( ) // keypress code"
Thank you! |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Sep 27, 2009 11:09 pm Post subject: |
|
|
Do you realize that you're not doing anything with that code?
It only sends to the active window, which you're not setting.
Use GetAsyncKeyState() for your hotkey.
http://www.pinvoke.net/default.aspx/user32/GetAsyncKeyState.html
Just spawn a timer or thread to check for it.
| Code: | if(GetAsyncKeyState(...))
{
//send a key
} |
|
|
| Back to top |
|
 |
darckalan Newbie cheater
Reputation: 0
Joined: 27 Sep 2009 Posts: 14
|
Posted: Sun Sep 27, 2009 11:16 pm Post subject: |
|
|
| slovach wrote: | Do you realize that you're not doing anything with that code?
It only sends to the active window, which you're not setting.
Use GetAsyncKeyState() for your hotkey.
Just spawn a timer or thread to check for it.
| Code: | if(GetAsyncKeyState(...))
{
//send a key
} |
|
Program will be made specifically for ONE game.
No need to be used outside the game.
thats why I use specific process. |
|
| Back to top |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Mon Sep 28, 2009 2:35 am Post subject: |
|
|
| darckalan wrote: | Program will be made specifically for ONE game.
No need to be used outside the game.
thats why I use specific process. |
lol i don't think you understand. slovach's tell you that this:
| Code: | | IntPtr hWnd = FindWindow(null, "MyProcessName"); |
is doing nothing. you're giving the variable hWnd a value, but you're not using it. use PostMessage if you want to send to a specific window. i really can't see the logic behind how you were thinking... _________________
|
|
| Back to top |
|
 |
darckalan Newbie cheater
Reputation: 0
Joined: 27 Sep 2009 Posts: 14
|
Posted: Mon Sep 28, 2009 7:57 am Post subject: |
|
|
I am using this now...
| Code: | private void autoHPToolStripMenuItem_Click(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("notepad");
foreach (Process p in processes)
{
do
{
Thread.Sleep(100);
SendKeys.Send("{Q}");
}
while (true); // keypress code
} |
But the application freezes when I execute it... :S |
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Mon Sep 28, 2009 9:27 am Post subject: |
|
|
Ofcourse it freezes, you have an infinite loop in there... do you even know C# or coding at all?  |
|
| Back to top |
|
 |
XiO Newbie cheater
Reputation: 0
Joined: 27 Sep 2009 Posts: 22
|
Posted: Mon Sep 28, 2009 11:06 am Post subject: |
|
|
Did Dark Byte put a no being a dick head rule? Because, back in the day, someone would have came in and chewed your ass out and said "You need to go read the MSDN FIRST!!!!" or "Learn to code." |
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Mon Sep 28, 2009 2:38 pm Post subject: |
|
|
| FullyAwesome wrote: | | darckalan wrote: | Program will be made specifically for ONE game.
No need to be used outside the game.
thats why I use specific process. |
lol i don't think you understand. slovach's tell you that this:
| Code: | | IntPtr hWnd = FindWindow(null, "MyProcessName"); |
is doing nothing. you're giving the variable hWnd a value, but you're not using it. use PostMessage if you want to send to a specific window. i really can't see the logic behind how you were thinking... | Urhm, not every game checks the messages that are sent.
| darckalan wrote: | I am using this now...
| Code: | private void autoHPToolStripMenuItem_Click(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("notepad");
foreach (Process p in processes)
{
do
{
Thread.Sleep(100);
SendKeys.Send("{Q}");
}
while (true); // keypress code
} |
But the application freezes when I execute it... :S |
Don't you understand the code? For each process that has the name "notepad.exe" it sends a "Q" to the active window. Also doing a do{ }while(true); will put the program into a infinite loop, that would explain the freezing since you're doing a constant loop on the program's main thread. I suggest you go to here: http://dotnetperls.com/backgroundworker or http://msdn.microsoft.com/en-us/library/aa645740%28VS.71%29.aspx. |
|
| Back to top |
|
 |
darckalan Newbie cheater
Reputation: 0
Joined: 27 Sep 2009 Posts: 14
|
Posted: Mon Sep 28, 2009 3:22 pm Post subject: |
|
|
Ok thank you!
You dont need to talk like my dad, I just started on this... C# is my first language... |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Sep 28, 2009 10:10 pm Post subject: |
|
|
| darckalan wrote: | Ok thank you!
You dont need to talk like my dad, I just started on this... C# is my first language... |
and it would be a lot less painful if you actually read a book / tutorial that actually taught you. |
|
| Back to top |
|
 |
|