News:

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

process keyboard input

Started by glen22, May 09, 2011, 07:44:36 AM

Previous topic - Next topic

glen22

hi
i want to write a little windows game -in a dialog form, but i don't know how to get input from user -from keyboard
i don't want to use getasynckeystate, i want to process the keyboard input like a normal video game -say assassins creed or final fantasy
by the way, how are these video games process the input? do they install a keyb hook, or what?
some code would be great
thank you for your time

Tedd

No snowflake in an avalanche feels responsible.

dedndave

a keyboard hook is simpler to impement that it appears   :P
MicahelW showed me an example, which i can't find now
but, here is a working example that i wrote afterward...
http://www.masm32.com/board/index.php?topic=16480.msg136801#msg136801

the program attached to this post has a little more advanced version...
http://www.masm32.com/board/index.php?topic=16480.msg137207#msg137207

baltoro

If you are using some version of DirectX for the graphics, using DirectInput for user keyboard and mouse input is pretty easy.
Baltoro

MichaelW

The problem with GetKeyboardState is that it's slow.

;==============================================================================
    include \masm32\include\masm32rt.inc
    .686
    include \masm32\macros\timers.asm
;==============================================================================
    .data
      keyState db 256 dup(0)
    .code
;==============================================================================
start:
;==============================================================================
    invoke GetKeyboardState, ADDR keyState
    print str$(eax),13,10,13,10

    invoke Sleep, 3000

    REPEAT 3

        counter_begin 1000, HIGH_PRIORITY_CLASS
        counter_end
        print str$(eax), 9, "cycles, empty",13,10

        counter_begin 1000, HIGH_PRIORITY_CLASS
            invoke GetAsyncKeyState, VK_ESCAPE
        counter_end
        print str$(eax), 9, "cycles, GetAsyncKeyState",13,10

        counter_begin 1000, HIGH_PRIORITY_CLASS
            invoke GetKeyboardState, ADDR keyState
        counter_end
        print str$(eax), 9, "cycles, GetKeyboardState",13,10,13,10

    ENDM

    inkey "Press any key to exit..."
    exit
;==============================================================================
end start


0       cycles, empty
54      cycles, GetAsyncKeyState
5419    cycles, GetKeyboardState

0       cycles, empty
41      cycles, GetAsyncKeyState
5396    cycles, GetKeyboardState

0       cycles, empty
41      cycles, GetAsyncKeyState
5404    cycles, GetKeyboardState


I would think that most games would need to respond to only a relatively small number of keys, nowhere near the 256 keys that GetKeyboardState reports the status of. If the goal is to respond to multiple overlapping key presses, then either function can be used do this. Assuming that these (console-mode) cycle counts are representative of what you would get in a graphics application, for anything less than about 100 keys GetAsyncKeyState would have less overhead.

eschew obfuscation

hfheatherfox07

Quote from: glen22 on May 09, 2011, 07:44:36 AM
hi
i want to write a little windows game -in a dialog form, but i don't know how to get input from user -from keyboard
i don't want to use getasynckeystate, i want to process the keyboard input like a normal video game -say assassins creed or final fantasy
by the way, how are these video games process the input? do they install a keyb hook, or what?
some code would be great
thank you for your time

The MASM forum is so full of knowledge we sometimes forget what's here....

here is an example of  a little windows game :

http://www.masm32.com/board/index.php?topic=6559.0

http://www.phoenixbit.com/site/projects/asmpacman/asmpacman.zip