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 


[question] GetAsyncKeyState (C#)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Sat Aug 09, 2008 7:25 am    Post subject: [question] GetAsyncKeyState (C#) Reply with quote

i want to use GetAsyncKeyState API through C# and i want that i'll press the key it will write it on console only 1 time but when i do this:
Code:

if (GetAsyncKeyState(Keys.A))
{
  // etc
}


it writes 'A' like 10 times
i saw that at C++ you write it with "&1" and then it write it only 1 time...
what's the way to use it at C# ?
10Q :]

_________________
Stylo
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Aug 09, 2008 8:04 am    Post subject: Reply with quote

Console.Read()?
_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Sat Aug 09, 2008 8:18 am    Post subject: Reply with quote

no no...
i mean i want to write the key only one time...
like:
Code:

for (;;)
{
 if (GetAsyncKeyState(Keys.A))
{
 Console.WriteLine("A key pressed");
}
 System.Threading.Thread.Sleep(1);
}


now when u press A it will print it like 10 or more times...
how can i make it to print it only 1 time?

_________________
Stylo
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Sat Aug 09, 2008 8:22 am    Post subject: Reply with quote

Put a sleep command.
Code:
Sleep(500);
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Sat Aug 09, 2008 8:32 am    Post subject: Reply with quote

i tried that but then it makes a delay and i hate it... isn't there a way to make it like in C++ with the "&1" thing?
_________________
Stylo
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Aug 09, 2008 8:39 am    Post subject: Reply with quote

1qaz wrote:
no no...
i mean i want to write the key only one time...
like:
Code:

for (;;)
{
 if (GetAsyncKeyState(Keys.A))
{
 Console.WriteLine("A key pressed");
}
 System.Threading.Thread.Sleep(1);
}


now when u press A it will print it like 10 or more times...
how can i make it to print it only 1 time?


I don't understand what's wrong with

Code:

if (Console.Read() == 'A')
{
    Console.WriteLine("A key pressed");
}

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Aug 09, 2008 8:51 am    Post subject: Reply with quote

He/She is monitoring for a key and when it gets pressed it is looping so fast it gets counted as being pressed multiple times.

You can fix this by doing an 'AND' operation to the return value.

Code:
if( GetAsyncKeyState(Keys.A)&1 )


I'm not 100% sure that will work in C# but you need to do something of that sort.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Aug 09, 2008 9:11 am    Post subject: Reply with quote

Ah, so this is for a trainer or something of the like, that makes more sense.

You may also want to increase the sleep by a little bit.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Sat Aug 09, 2008 10:42 am    Post subject: Reply with quote

isn't there other way to do it instead of "Sleep" ?
all i'm looking for is a same way as C++ with the "&1" but it doesn't work at C#

_________________
Stylo
Back to top
View user's profile Send private message
chaseC
Newbie cheater
Reputation: 0

Joined: 08 Jul 2008
Posts: 10

PostPosted: Sat Aug 09, 2008 11:17 am    Post subject: Reply with quote

you could add a loop that does nothing while "a" is pressed, but if you intentionally hold it down to print more then one "a" it won't work

try this:
Code:

for (;;)
{
 if (GetAsyncKeyState(Keys.A))
{
 Console.WriteLine("A key pressed");
 while(GetAsyncKeyState(Keys.A)){}
}
 System.Threading.Thread.Sleep(1);
}
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