Author Topic: About GetAsyncKeyState  (Read 381 times)

felipe

  • Member
  • ****
  • Posts: 943
  • Eagles are just great!
About GetAsyncKeyState
« on: April 27, 2018, 12:50:47 PM »
I have realized that this function as microsoft documents, returns a short. I have being using this function as such before  :redface::
Code: [Select]
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:
Code: [Select]
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.

felipe

  • Member
  • ****
  • Posts: 943
  • Eagles are just great!
Re: About GetAsyncKeyState
« Reply #1 on: April 27, 2018, 01:26:12 PM »
Well, Olly have told me that in fact it's implemented at some point as:
Code: [Select]
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:
Code: [Select]
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.
Felipe.

jj2007

  • Member
  • *****
  • Posts: 8773
  • Assembler is fun ;-)
    • MasmBasic
Re: About GetAsyncKeyState
« Reply #2 on: April 27, 2018, 04:54:00 PM »
Code: [Select]
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

AW

  • Member
  • *****
  • Posts: 1500
  • Let's Make ASM Great Again!
Re: About GetAsyncKeyState
« Reply #3 on: April 27, 2018, 09:04:49 PM »
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--

  • Administrator
  • Member
  • ******
  • Posts: 5831
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: About GetAsyncKeyState
« Reply #4 on: April 28, 2018, 01:47:33 AM »
 :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
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: