News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Test for keypress

Started by Neil, June 18, 2008, 01:01:51 PM

Previous topic - Next topic

Neil

Tedd,

Done some more testing & it does exactly what I need :U

jj2007

Quote from: Tedd on June 19, 2008, 01:06:05 PM
Quote from: jj2007 on June 19, 2008, 12:49:39 PM
SHORT GetAsyncKeyState...  :wink
The documentation wasn't updated so attentively, luckily the code was - if in doubt, test it :P


I tested it, and you are right while Microsoft is wrong :cheekygreen:

Neil

I've had to come back to this one here is the code as I thought it was O.K.

startloop:
    ;some code

    invoke GetKeyState, VK_ESCAPE
    test eax,80000000h
    jz nextkey
    jmp exit

nextkey:
    invoke GetKeyState, VK_SPACE 
    test eax,80000000h  ;key down?
    jz startloop

    ;more code decrement counter & keep looping until zero

What I hadn't thought about at the time was what happens if you hold down the (Spacebar) key. When you do that the key is down each pass through the loop, not what's required. I then modified the code to this:-

startloop:
    ;some code

    invoke GetKeyState, VK_ESCAPE
    test eax,80000000h
    jz nextkey  ;key down?
    jmp exit

nextkey:
    invoke GetKeyState, VK_SPACE
    test eax,80000000h  ;key up?
    jnz startloop

    invoke GetKeyState, VK_SPACE
    test eax,80000000h  ;key down?
    jz startloop

    ;more code decrement counter & keep looping until zero

This cured holding down the key but introduced another snag, the keydown was caught only 50% of the time probably make a user very frustrated. This was cured by introducing a small delay :-


startloop:
    ;some code

    invoke GetKeyState, VK_ESCAPE
    test eax,80000000h  ;key down?
    jz nextkey
    jmp exit

nextkey:
    invoke GetKeyState, VK_SPACE
    test eax,80000000h  ;key up?
    jnz startloop

    invoke Sleep,150

    invoke GetKeyState, VK_SPACE
    test eax,80000000h  ;key down?
    jz startloop

    ;morecode decrement counter & keep looping until zero

This cured that problem, now  this code works perfectly if the spacebar is pressed & released around once per second or slower, any faster than that then it crashes & Microsoft wants me to send in the usual report. Anyone got any idea how to get around this one?


Neil

 :( Worse still, it only crashes some of the time making it more difficult to pin down

Tedd

What you need to do is keep a table with the known state of all the keys you want to check, then when you check you can see if the key is already down and ignore it so you don't count its repeats again.
So, when the table starts saying that all the wanted keys are up, then when you find that a key is down you say that in the table. If you find a key is down, but it's already down in the table, you ignore it. When a key is up again, you say it's up in the table (so the next time it's down it won't be ignored.)
(And no need for two separate checks for up and down - that can actually cause problems - if it's not down it must be up!)


Also, just a tip: when you have something like..

  jz nextkey
  jmp exit
nextkey:

..it makes more sense to do..

  jnz exit
nextkey:
No snowflake in an avalanche feels responsible.

Neil

Thanks for the advice Tedd, I'll look into that