| View previous topic :: View next topic |
| Author |
Message |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sat Aug 09, 2008 7:25 am Post subject: [question] GetAsyncKeyState (C#) |
|
|
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 |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Aug 09, 2008 8:04 am Post subject: |
|
|
Console.Read()?
_________________
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sat Aug 09, 2008 8:18 am Post subject: |
|
|
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 |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Sat Aug 09, 2008 8:22 am Post subject: |
|
|
| Put a sleep command.
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sat Aug 09, 2008 8:32 am Post subject: |
|
|
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 |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Aug 09, 2008 8:39 am Post subject: |
|
|
| 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");
}
|
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Aug 09, 2008 8:51 am Post subject: |
|
|
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 |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Aug 09, 2008 9:11 am Post subject: |
|
|
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.
_________________
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sat Aug 09, 2008 10:42 am Post subject: |
|
|
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 |
|
 |
chaseC Newbie cheater
Reputation: 0
Joined: 08 Jul 2008 Posts: 10
|
Posted: Sat Aug 09, 2008 11:17 am Post subject: |
|
|
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 |
|
 |
|