GetAsyncKeyState Replicate KeyHit [C++]

Started by Dabz, December 16, 2024, 14:40:53

Previous topic - Next topic

Dabz

Right, this has my head like a pissed bed for whatever reason... Should be easy but I cannot see it... Anyway, Blitzy "KeyDown", pimps, but for whatever reason, I cannot fathom how to replicate "KeyHit", and it's getting on my nerves now! :D

SHORT keyState;

for (int loop = VM_LBUTTON; loop <= VK_OEM_CLEAR; loop++)
{
    keyState = GetAsyncKeyState(loop);
    if (keyState & 0x8000)
    {
        if (keyDownHitState[loop] == true) //The button has been press previously.
        {
            if (keyDownState[loop] == true) //Previously down, but perform "The hit"
            {
                keyDownHitState[loop] = false;
            }
            else
            {
                keyDownHitState[loop] = true;
            }
        }
        keyDownState[loop] = true;
    }
    else
    {
        keyDownState[loop] = false;
    }
}

I don't want to rely on the least significant bit in the return value of GetAsyncKeyState, as that states the key has been pressed since the last call to the function, which would help, but it's deemed unreliable.

So I'm swinging with the arrays I'm using to map the keypresses... So, anyone fancy taking the soiled mattress out of my mind and throwing it outside to dry and help me make sense of it! :D lol

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

Dabz

Basically, I had myself a biffy baff, bit of a soak, and came back... Cracked it within 15 minutes:

for (int loop = VM_LBUTTON; loop <= VK_OEM_CLEAR; loop++)
{
oldKeyDownHitState[loop] = keyDownHitState[loop];
}

for (int loop = VM_LBUTTON; loop <= VK_OEM_CLEAR; loop++)
{
SHORT keyState = GetAsyncKeyState(loop);
if (keyState & 0x8000)
{
if (keyDownState[loop] == true && oldKeyDownHitState[loop] == false)
{
keyDownHitState[loop] = false;
}
else
{
keyDownHitState[loop] = true;
}
keyDownState[loop] = true;
}
else
{
keyDownState[loop] = false;
}
}

for (int loop = VM_LBUTTON; loop <= VK_OEM_CLEAR; loop++)
{
if (oldKeyDownHitState[loop] == true && keyDownHitState[loop] == true)
{
keyDownHitState[loop] = false;
}
}

An extra array to compare before and after was the key... Amazing what you can do with a bit of a fresher mind, you know, getting off your rocker on Radox's lavender and sea minerals bubble bath! :D lol

Honestly, that had me head like an absolute turnip la... Still cannot believe how long it took me to "Get it", simplest thing when you look!

Right, onwards and upwards... As they say... In... Erm... Lifts! :P

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

col

Hey Dabz. I notice that you're looping through 256 vk values to get the complete keyboard state. Just an FYI that there is a GetKeyboardState win32 function the will do the same without looping. Not sure if it fits your use case however.

If you are getting the key state for a window there is a more efficient (in terms of execution speed) way to do this via the WndProc.
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Dabz

Hey col, good morning fine sir...

Well, GetKeyBoardState relies on getting the states when events are processed, so you may as well do that in the window procedure anyway, where GetAsyncKeyState reports the states for the here and now, but that's immaterial for the problem due to getting the states was never the issue, the issue was to replicate the logic behind a KeyHit affair, regardless of where the states come from.

I did start it off in the windows callback procedure checking WParam, and what made me go to GetAsyncKeyState was you can check if the state of the key had been held down in the previous call by checking the least sig-bit in the return value... Which was what I was after, but then I discovered it's not reliable and deemed unsafe, so had to think of another way.

In fact, this is what I'm annoyed with myself for, because I needed a record of the previous state before checking the current state, the answer was there, it had neon lights, disco balls, fire works and a super moon... All I needed to do was copy the bloody current states before checking for updates then comparing them!

One of them moments I called myself a complete tit! :D

Anyway, before I went to bed, I moved it to the the call back function and checking wParam again, and it's working lovely! :)

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

Baggey

#4
Ive gone round in circles sometimes for weeks, and all of a sudden "Smoke me a Kipper" i see my stupid mistake! You Tit ;D

I cant say what i called myself :-X

Glad to see your progressing!

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!