Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] SendKeys unfocused

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
darckalan
Newbie cheater
Reputation: 0

Joined: 27 Sep 2009
Posts: 14

PostPosted: Sun Sep 27, 2009 8:02 pm    Post subject: [C#] SendKeys unfocused Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Sep 27, 2009 9:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
darckalan
Newbie cheater
Reputation: 0

Joined: 27 Sep 2009
Posts: 14

PostPosted: Sun Sep 27, 2009 10:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Sep 27, 2009 11:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
darckalan
Newbie cheater
Reputation: 0

Joined: 27 Sep 2009
Posts: 14

PostPosted: Sun Sep 27, 2009 11:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
FullyAwesome
I post too much
Reputation: 0

Joined: 05 Apr 2007
Posts: 4438
Location: Land Down Under

PostPosted: Mon Sep 28, 2009 2:35 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
darckalan
Newbie cheater
Reputation: 0

Joined: 27 Sep 2009
Posts: 14

PostPosted: Mon Sep 28, 2009 7:57 am    Post subject: Reply with quote

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
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Mon Sep 28, 2009 9:27 am    Post subject: Reply with quote

Ofcourse it freezes, you have an infinite loop in there... do you even know C# or coding at all? Confused
Back to top
View user's profile Send private message
XiO
Newbie cheater
Reputation: 0

Joined: 27 Sep 2009
Posts: 22

PostPosted: Mon Sep 28, 2009 11:06 am    Post subject: Reply with quote

Laughing 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
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Mon Sep 28, 2009 2:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
darckalan
Newbie cheater
Reputation: 0

Joined: 27 Sep 2009
Posts: 14

PostPosted: Mon Sep 28, 2009 3:22 pm    Post subject: Reply with quote

Ok thank you!

You dont need to talk like my dad, I just started on this... C# is my first language...
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Sep 28, 2009 10:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites