The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: hutch-- on October 19, 2008, 09:23:59 PM

Title: API based console menu technique.
Post by: hutch-- on October 19, 2008, 09:23:59 PM
The MSVCRT functions have worked fine in this area but here is a simpler Windows API method that seems to work OK as well using GetAsynchKeyState().


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    iskey MACRO vk_key
      invoke GetAsyncKeyState,vk_key
      not eax
      test eax, 00000000000000001000000000000000b
    ENDM

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    push esi

    invoke GetTickCount
    add eax, 5000                   ; add 5 seconds for timeout
    mov esi, eax

    print "Press 1 to 5 or ESCAPE to quit",13,10

  ; ---------------------------------

  stlbl:
    invoke SleepEx,1,0              ; yield while idling
    invoke GetTickCount
    cmp esi, eax                    ; check timeout threshold
    jle timeout

    iskey VK_1
    je lbl1

    iskey VK_2
    je lbl2

    iskey VK_3
    je lbl3

    iskey VK_4
    je lbl4

    iskey VK_5
    je lbl5

    iskey VK_ESCAPE                 ; loop back if ESCAPE not pressed
    jne stlbl

  ; ---------------------------------

    print "You pressed ESCAPE",13,10
    jmp quit

  lbl1:
    print "You pressed 1",13,10
    jmp quit

  lbl2:
    print "You pressed 2",13,10
    jmp quit

  lbl3:
    print "You pressed 3",13,10
    jmp quit

  lbl4:
    print "You pressed 4",13,10
    jmp quit

  lbl5:
    print "You pressed 5",13,10
    jmp quit

  timeout:
    print "Loop timed out",13,10

  quit:
    pop esi

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Title: Re: API based console menu technique.
Post by: Neil on October 20, 2008, 08:18:49 AM
hutch,
can you explain why you are using the NOT operator & testing the bit you are because when I use GetAsyncKeyState I test the Hi bit of eax to see if a key is down & the Lo bit to see if it is up, I,m a little puzzled?
Title: Re: API based console menu technique.
Post by: hutch-- on October 20, 2008, 08:25:53 AM
Neil,

I used the NOT mnemonic to invert the required conditional jump so that the semantics matched the logic.


    iskey VK_1  ; if the pressed key = VK_1
    je lbl1     ; jump to label


Without the NOT mnemonic you must use the inverse jump "jne" which is counter intuitive to code.

The bit I am testing handles a key held down where you have a repeat of the key.
Title: Re: API based console menu technique.
Post by: Neil on October 20, 2008, 08:35:41 AM
Thanks hutch,
Got it  :thumbu
Title: Re: API based console menu technique.
Post by: Vortex on October 20, 2008, 04:54:51 PM
Hi Hutch,

Thanks for this example :U