News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

About GetAsyncKeyState

Started by felipe, April 27, 2018, 12:50:47 PM

Previous topic - Next topic

felipe

I have realized that this function as microsoft documents, returns a short. I have being using this function as such before  :redface::

push VK_LEFT
call    GetAsyncKeyState

test   eax,80000000h
jnz    go_to_left                          ; Left key pressed.


And worked fine   :shock:. So i'm guessing that microsoft win api internally code something like:

push VK_LEFT
call    GetAsyncKeyState

movsx eax,ax
test   eax,80000000h
jnz    go_to_left                          ; Left key pressed.


Also i have found in the windows.inc that the typedef for SHORT it's commented. It's because can have any conflict with other type? Or is just better to only typedef USHORT? Well, i will not use any of this typedef anyway, is just curiosity.  :idea:

felipe

Well, Olly have told me that in fact it's implemented at some point as:

movsx eax,al


Well, at least for a console program (not using unicode). So, i think it will be much safer to do this in my code:

movsx eax,ax


As the microsoft documentation about the function states (returns a short). Is the safer approach, we don't know when it will change the code for the internally implementation of the win api.

jj2007

include \masm32\include\masm32rt.inc

.code
start:
  print "Press Ctrl C to exit", 13, 10, 10
  .While 1
invoke Sleep, 1
invoke GetAsyncKeyState, VK_SHIFT
test ah, ah
.if Sign?
print "Shift is pressed ", 13
.else
print "Shift not pressed", 13
.endif
  .Endw
end start

aw27

If Microsoft says it returns a short it means that it returns a short - period, in other words anything after the 16 bits is not part of the official solution, although may work for some versions of windows, eventually for all versions of windows. Confused?  :lol:
However, the 16-bit (the short part) has the obligation to always work as per the specification.

hutch--

 :biggrin:

Has worked well for many years.

      IfRepeatKey MACRO vk_key
        invoke GetAsyncKeyState,vk_key
        and eax, 00000000000000001000000000000000b
        rol eax, 17
        cmp eax, 1
      ENDM