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

Is there a way of testing if a key has been pressed without having to wait for it.

Tedd

No snowflake in an avalanche feels responsible.

Neil

Thanks for the link Tedd.
I've had a look at it & from what I understand is that all I've got to do is call that API & test the Hi bit of eax. If it's set then get that key from the buffer & act on it, correct me if I'm wrong.

jj2007

GetKeystate is typically used in combination with a WM_KEYDOWN message to check whether Shift or Control are pressed.

>test the Hi bit of eax

Watch your step. It's actually the high bit of ax...

Neil

Thanks for the info jj2007

hutch--

Neil,

I use this type of code for processing hotkeys in the message loop with a WM_KEYUP message.


            invoke GetAsyncKeyState,VK_CONTROL
            rol eax, 16
            cmp ax, 1111111111111111b
            jne hkout


Tell us this much, are you talking about a console app or a GUI app ?
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd

Quote from: Neil on June 18, 2008, 02:15:30 PM
I've had a look at it & from what I understand is that all I've got to do is call that API & test the Hi bit of eax. If it's set then get that key from the buffer & act on it, correct me if I'm wrong.

Call the function when you want to check whether the key has been pressed, then test the high bit of eax.
You don't really need to 'get' the key, since you already know it was pressed (since you're the one checking it); any extra keypress messages will be eaten by DefaultWindowProc.

There's a compatibility issue - as jj says, test the high bit of ax. It's a rewritten 16-bit function, so the return value used to be a short, meaning it 'was' the high bit of ax, but now the correct one is the high bit of eax. But, all high bits get set for this reason - so either will work.

GetAsyncKeyState is for checking whether the key is down 'right now.'
No snowflake in an avalanche feels responsible.

jj2007

Quote from: hutch-- on June 19, 2008, 12:29:26 PM
I use this type of code for processing hotkeys in the message loop with a WM_KEYUP message.


            invoke GetAsyncKeyState,VK_CONTROL
            rol eax, 16
            cmp ax, 1111111111111111b
            jne hkout

Win32hlp: 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. The return value is zero if a window in another thread or process currently has the keyboard focus.

"return value is zero" is imho a bit misleading, because the classic use of GetAsyncKeyState is to check whether in the active thread, not your own thread, somebody has pressed a key.

Quote from: Tedd on June 19, 2008, 12:43:15 PM
There's a compatibility issue - as jj says, test the high bit of ax. It's a rewritten 16-bit function, so the return value used to be a short, meaning it 'was' the high bit of ax, but now the correct one is the high bit of eax. But, all high bits get set for this reason - so either will work.
MSDN: SHORT GetAsyncKeyState...  :wink

Tedd

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
No snowflake in an avalanche feels responsible.

PBrennick

Hutch and I have been working on our editors for a long time so we can assure you that his method is sound. The only difference I would suggest is that the test should be only done when the transitional state is 'UP'. For example:

    .if msg.message == WM_KEYDOWN   ; Was a key pressed?
      .if msg.wParam == VK_ESCAPE   ; Escape key?
      .endif
    .endif
    .if msg.message == WM_KEYUP
    ; Function keys should be processed here
      .if msg.wParam == VK_F3

      .elseif msg.wParam == VK_F4

      .elseif msg.wParam == VK_F5

      .endif
      invoke  GetAsyncKeyState, VK_CONTROL
      rol     eax, 16
      cmp     ax, 1111111111111111b
      jne     ProcessMessage ; Enter the message loop

      .if msg.wParam == 46h             ; Ctrl + F
        ; Take action on this hotkey
      .endif
      ; ...more of the same

    .endif


Works like a charm. Take close note on the difference between a function key and a control key sequence.

hth,
-- Pal
The GeneSys Project is available from:
The Repository or My crappy website

Neil

hutch, I'm using it in a console app.

I've read the other replies & I still haven't got my head around it yet, but I'm working on it.

Basically what I'm trying to do is run a loop & each pass through I want to know if one of either 2 keys has been pressed, one to break out of the loop i.e, cancel it & the other to run some more code which either breaks out of the loop if some condition is met or if not loop again. I achieved this in my DOS days by checking if a key was in the keyboard buffer at bottom of memory, but obviously I need another method now.

hutch--

Neil,

The stuff above is for a normal GUI app, console is another planet. If you are just after the functionality, have a look at the two macros "inkey" and "getkey" which both use the MSVCRT library to provide the functionality. Greg Lyon wrote both of these are they have been very reliable.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd


invoke GetKeyState, VK_CONTROL    ;or whichever key you want to check
test eax,80000000h
jz @F

;key was pressed - do whatever

@@:


It doesn't need to be done in response to WM_KEYDOWN/UP - so it should work for a console window too (not that I can test this right now :bdg)
No snowflake in an avalanche feels responsible.

Neil

Thanks hutch,

I'm familiar with the macros & both wait for a keypress which is not what I want.

Tedd that looks promising

Neil

Tedd,

Tested that piece of code & it appears to work O.K.  Got to do some more testing before I'm completely sure.