How can I log key presses in Game Maker?

| | August 8, 2015

I’m trying to create a log of a players actions as they play a game of Spelunky. The easiest I’ve found to do this is to log what keys are pressed at each frame. What I don’t know how to do is how to integrate this with the Game Maker source code of Spelunky.

Is there a specific way to create a script that is checked every frame/tick (don’t know the right term) and a command to find what buttons are pressed?

2 Responses to “How can I log key presses in Game Maker?”

  1. Spencer Killen on November 30, -0001 @ 12:00 AM

    What might Be even simple but it could go bad, is if you logged the x and y of the character and stored then in a 2d array [frame,x or y Boolean value] then just repeat the sequence

  2. I would store the keys and times as two ds_lists.

    Create an object and call it keyLogger (or whatever). In the create event, create two lists:

    keyCodes = ds_list_create();
    keyTimes = ds_list_create();
    frame = 0;
    

    Then, in the step event, you can log the keys and advance time.

    if(keyboard_check_pressed(vk_anykey))
    {
        ds_list_add(keyCodes, keyboard_key);
        ds_list_add(keyTimes, frame);
    }
    frame += 1;
    

    You can then draw all the keys in the draw event.

    var i, s;
    for(i = 0; i < ds_list_size(keyCodes); i += 1)
    {
        s = "At frame ";
        s += string(ds_list_find_value(keyTimes, i));
        s += " you pressed key ";
        s += chr(ds_list_find_value(keyCodes, i));
        s += " (Code: ";
        s += string(ds_list_find_value(keyCodes, i));
        s += ")."
        draw_text(4, 4+i*20, s);
    }
    

    All you need to do is put this object in any room that you want to log keys in.

    It should give an output that looks like this:

    At frame 159 you pressed key F (Code: 14).

    At frame 232 you pressed key 7 (Code: 95).

Leave a Reply