HelpIf (GetAsyncKeyState && Label Forecolor)

Posts 1–8 of 8 · Page 1 of 1
If (GetAsyncKeyState && Label Forecolor)
Hi EveryBody ;

For Visual Basic ;

If GetAsyncKeyState (Keys.Down) And Label1.Forecolor = Color.Green Then

Label1.ForeColor = Color.Red
Label2.ForeColor = Color.Green

Elseif GetAsyncKeyState (Keys.Down) And Label2.Forecolor = Color.Green Then

Label2.ForeColor = Color.Red
Label3.ForeColor = Color.Green

End If


For C++ ;

if(GetAsyncKeyState(VK_DOWN) && Label1->ForeColor = System:rawing::Color::Green
{
Label1.Forecolor -> ...
Label2.Forecolor -> ....
}




But , && ; Operand SHORT && System:rawing::Color Issiues

How Can I Do It ?
You can't implicitly convert a short to a boolean... (well, in C++ you can).

If (GetAsyncKeyState(Keys.Down) And &H1) = &H1 AndAlso Label1.Forecolor = Color.Green Then

if((GetAsyncKeyState(VK_DOWN) & 1) == 1 && Label1->ForeColor == System:rawing::Color::Green)
Quote Originally Posted by master131 View Post
You can't implicitly convert a short to a boolean... (well, in C++ you can).

If (GetAsyncKeyState(Keys.Down) And &H1) = &H1 AndAlso Label1.Forecolor = Color.Green Then

if((GetAsyncKeyState(VK_DOWN) & 1) == 1 && Label1->ForeColor == System:rawing::Color::Green)
You should really be testing against the most significant bit, not the least.

i.e

Code:
If (GetAsyncKeyState(Keys.Down) And &H8000) <> 0 AndAlso Label1.Forecolor = Color.Green Then

if((GetAsyncKeyState(VK_DOWN) & 0x8000) && Label1->ForeColor == System::Drawing::Color::Green)
From MSDN:
Quote Originally Posted by msdn
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

Remarks:
Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.
So yeah LSB is less reliable and is really only there for backwards compatibility, the MSB check is more accurate.
Quote Originally Posted by master131 View Post
You can't implicitly convert a short to a boolean... (well, in C++ you can).
You can change the P/Invoke to a boolean.. always worked out for me
C#
Code:
[DllImport("user32.dll")]
static extern bool GetAsyncKeyState(System.Windows.Forms.Keys vKey);
VB
Code:
<DllImport("user32.dll")> _
Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Boolean
End Function


And @Jason, correct if I'm wrong but doing an AND with 0x8000 will actually check if the key is down at the current moment. 0x1 checks if it was pressed since the last call to GetAsyncKeyState. Usually, the last option is better for loops cause the user might have pressed the key to, let's say, change the select option on a menu, when you're drawing the menu, so when you actually call GetAsyncKeyState the key is not down anymore, but it was down a few milliseconds ago.
Quote Originally Posted by MarkHC View Post
You can change the P/Invoke to a boolean.. always worked out for me
C#
Code:
[DllImport("user32.dll")]
static extern bool GetAsyncKeyState(System.Windows.Forms.Keys vKey);
VB
Code:
<DllImport("user32.dll")> _
Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Boolean
End Function


And @Jason, correct if I'm wrong but doing an AND with 0x8000 will actually check if the key is down at the current moment. 0x1 checks if it was pressed since the last call to GetAsyncKeyState. Usually, the last option is better for loops cause the user might have pressed the key to, let's say, change the select option on a menu, when you're drawing the menu, so when you actually call GetAsyncKeyState the key is not down anymore, but it was down a few milliseconds ago.
Did you even read the MSDN reference mate?
Quote Originally Posted by Jason View Post


Did you even read the MSDN reference mate?
that's why I said correct if I'm wrong
Quote Originally Posted by MarkHC View Post

that's why I said correct if I'm wrong
If you only check the lower bit, another application may call GetAsyncKeyState before you and unset the lower bit. For example, if you have 2 concurrent applications that are both polling GetAsyncKeyState for key presses, potentially half of the user's keypresses will go undetected by either one application or the other as the other app may have "stolen" the bit. AFAIK, even if the applications aren't checking the same key, the lower bit will still be unset regardless.

You *could* check both the low bit and high bit if you want, though.
Code:
if (GetAsyncKeyState(VK_INSERT) & 0x8001)
{
    // either the key is down now, or the user pressed it since the last call to GetAsyncKeyState.
}
Not Working Two Methods
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us