Salad7 wrote: |
---|
I want to create a toggle method using the insert key as an on and off switch |
I'm not sure I understand if you want to create a function that gets called every time the key state changes, or if you want a function that gets called repeatedly and figures out if the state has changed.
GetAsyncKeyState
I'm assuming you're using the Windows Desktop API, since you show the
GetAsyncKeyState
function.
Documentation for
GetAsyncKeyState
is here, and (hopefully) has all you need to know:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx
You have given it an appropriate
virtual-key code. Its return code is
0
when an error occurs, so the code to check the key state would be where the
?
is in your code.
According to the documentation,
GetAsyncKeyState
returns a
SHORT
. The
most-significant bit in the return code is set if the key is down. (The least-significant bit is set if the state has changed since the last call to
GetAsyncKeyState
, but the documentation recommends against relying on this behaviour.)
You would need to add code to keep the return value for use, e.g.:
1 2 3 4 5
|
SHORT keyState;
// . . .
if (keyState = GetAsyncKeyState(VK_INSERT))
{
// ? . . .
|
And then
inside that condition (the following case doesn't hold if the function returns an error), check the return value for the most-significant bit (
SHORT
is a signed integer type, so it is negative set if the most-significant bit is set), e.g.:
1 2 3 4 5 6 7 8
|
if (keyState < 0) // If most-significant bit is set...
{
// key is down . . .
}
else
{
// key is up . . .
}
|
Finally, to check for when the state toggles I advise you keep a
bool insertKeyWasDown = false;
and use that to record whether the last state sampled was down or up, if the state has changed then call your toggle function.
As a key-down message
Alternatively, you could handle the
WM_KEYDOWN
message in your
Window Procedure:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
Handling the message may well be more appropriate than polling the state in your application, and the message includes information on whether the button has moved since the last message (so you can avoid listening to auto-repeat key presses).