• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Pressing Shift completely resets binds

I have managed controls customization for my game, but when game window is not active, pressing Shift cause all binds reset.
No idea what could cause this and where to search. Is it possible to not be a code related problem?
 

Roldy

Member
Is it possible to not be a code related problem?
Doubtful.

No idea what could cause this and where to search.
Look at your code where you set/unset your key binds. Put a line similar to this:
GML:
show_debug_message(debug_get_callstack(0)[0]);
at various locations and see what get s called when this happens.
 
Doubtful.



Look at your code where you set/unset your key binds. Put a line similar to this:
GML:
show_debug_message(debug_get_callstack(0)[0]);
at various locations and see what get s called when this happens.
Well, I found what gets called. To be short pressing shift binds each control and replace duplicate, in the end I have nothing set.
I could simply fix this with window_has_focus(), but I am curios why thats happening and why exclusively with Shift

EDIT: I have nothing connected with shift in my code
 

Nidoking

Member
Well, I found what gets called. To be short pressing shift binds each control and replace duplicate, in the end I have nothing set.
Could you give the not-short version? It's great that you have information, but we don't, and I think you'll agree that having that information would make it much easier to help you.
 
Could you give the not-short version? It's great that you have information, but we don't, and I think you'll agree that having that information would make it much easier to help you.
Post the code that resets the controls and duplicates, or that could reset them.
Here is code for "Press Key - Any" event. The moment I press Shift message from "show_debug_message("6")" getting triggered 7 times (probably 1 time for each instance) and no other debug messages appear.

GML:
if keyboard_check_pressed(vk_escape) // Cancel
{
    show_debug_message("1");
    keySet = 0;
    keyDuplicate = 0;
    sprite_index = spr_Controls_Slot;
}
else
{
    if keySet == 1 // Sets to 1 by pressing LMB on instance
    {
        show_debug_message("2");
        keyPressed = keyboard_lastkey;
        if keyboard_lastkey != key_code // Search if one of controls has such binded key
        {
            show_debug_message("3");
            event_user(0); // Search for duplicates
        }
        if KeyDuplicateCheck != 0 // Calls if duplicate has been found
        {
            show_debug_message("4");
            keyDuplicate = script_execute(scr_KeycodeToString, keyboard_lastkey);
            keySet = 0;
        }
        else // Calls if no duplicates found
        {
            show_debug_message("5");
               key_string = script_execute(scr_KeycodeToString, keyboard_lastkey);
            key_code = keyboard_lastkey;
               sprite_index = spr_Controls_Slot;
          
            keySet = 0;
            keyDuplicate = 0;
            KeyDuplicateCheck = 0;
            keyPressed = 0;
            event_user(2); // Save changes
        }
    }
    else // Calls if duplicated key has been pressed again for confirmation
    {
        if keyPressed == keyboard_lastkey
        {
            show_debug_message("6");
            event_user(1); // Unbind control with same key
               key_string = script_execute(scr_KeycodeToString, keyboard_lastkey);
            key_code = keyboard_lastkey;
               sprite_index = spr_Controls_Slot;
              
            keySet = 0;
            keyDuplicate = 0;
            KeyDuplicateCheck = 0;
            keyPressed = 0;
            event_user(2); // Save changes
        }
    }
}
User event 1:
GML:
/// @description Remove Duplicate
for (i = 0; i < instance_count; i++)
{
    temp_id = instance_id_get(i);
    if temp_id.object_index == obj_Controls_Slot
    {
        if temp_id.key_code == keyboard_lastkey
        {
            temp_id.key_code = 0;
            temp_id.key_string = "Not Set";
            temp_id.sprite_index = spr_Controls_SlotEmpty;
        }
    }
}
 
Last edited:

Nidoking

Member
So, if any key is pressed that isn't Escape while keySet is not equal to one, and the same key is pressed again, every control (I assume this is in the object managing each control, since it accesses instance variables directly) loops through ALL of these instances and clears their keys, then accepts that key as its key_code. Then the next one also processes that keypress, clears the previous instances (which were just set to that same key in the same step), and then takes the new key as its key_code.

I think you need more checks to make sure that you are, actually, performing key set activities when you manipulate these keys.
 
So, if any key is pressed that isn't Escape while keySet is not equal to one, and the same key is pressed again, every control (I assume this is in the object managing each control, since it accesses instance variables directly) loops through ALL of these instances and clears their keys, then accepts that key as its key_code. Then the next one also processes that keypress, clears the previous instances (which were just set to that same key in the same step), and then takes the new key as its key_code.

I think you need more checks to make sure that you are, actually, performing key set activities when you manipulate these keys.
I had same thoughts, but its happening exclusively with Shift also when game window not active, that shouldn't happening? Also as instance at bottom states it got key_code or key_string "0"? Is that connected with Shift?
 

Nidoking

Member
I had same thoughts, but its happening exclusively with Shift also when game window not active, that shouldn't happening?
Have you tried pressing any other keys multiple times? Maybe, because Shift is a modifier, it works differently. You might get similar results with Ctrl or Alt.

Also as instance at bottom states it got key_code or key_string "0"? Is that connected with Shift?
I have no idea, because you haven't included the code for your scr_KeycodeToString.
 
Have you tried pressing any other keys multiple times? Maybe, because Shift is a modifier, it works differently. You might get similar results with Ctrl or Alt.
Yea tried before and tried once again now, works only with both left and right Shifts and pressing 1 time is enough.

I have no idea, because you haven't included the code for your scr_KeycodeToString.
GML:
/// key_to_string( key )
/*//
How to use:
Simply call this script in a draw_text function.
argument0 should be a keyboard key such as vk_enter or ord('Z').
//*/

if( argument0 > 48 && argument0 < 91 )
{ return chr(argument0); }
switch( argument0 )
{
    case -1: return "No Key";
    case 8: return "Backspace";
    case 9: return "Tab";
    case 13: return "Enter";
    case 16: return "Shift";
    case 17: return "Ctrl";
    case 18: return "Alt";
    case 19: return "Pause/Break";
    case 20: return "CAPS";
    case 27: return "Esc";
    case 32: return "Space";
    case 33: return "Page Up";
    case 34: return "Page Down";
    case 35: return "End";
    case 36: return "Home";
    case 37: return "Left Arrow";
    case 38: return "Up Arrow";
    case 39: return "Right Arrow";
    case 40: return "Down Arrow";
    case 45: return "Insert";
    case 46: return "Delete";
    case 96: return "Numpad 0";
    case 97: return "Numpad 1";
    case 98: return "Numpad 2";
    case 99: return "Numpad 3";
    case 100: return "Numpad 4";
    case 101: return "Numpad 5";
    case 102: return "Numpad 6";
    case 103: return "Numpad 7";
    case 104: return "Numpad 8";
    case 105: return "Numpad 9";
    case 106: return "Numpad *";
    case 107: return "Numpad +";
    case 109: return "Numpad -";
    case 110: return "Numpad .";
    case 111: return "Numpad /";
    case 112: return "F1";
    case 113: return "F2";
    case 114: return "F3";
    case 115: return "F4";
    case 116: return "F5";
    case 117: return "F6";
    case 118: return "F7";
    case 119: return "F8";
    case 120: return "F9";
    case 121: return "F10";
    case 122: return "F11";
    case 123: return "F12";
    case 144: return "Num Lock";
    case 145: return "Scroll Lock";
    case 186: return ";";
    case 187: return "=";
    case 188: return ",";
    case 189: return "-";
    case 190: return ".";
    case 191: return "/";
    case 192: return "`";
    case 219: return "/";
    case 220: return "[";
    case 221: return "]";
    case 222: return "'";
}
 
Have you tried pressing any other keys multiple times? Maybe, because Shift is a modifier, it works differently. You might get similar results with Ctrl or Alt.



I have no idea, because you haven't included the code for your scr_KeycodeToString.
I've noticed something. There is no "default:" in the script, I've added default: return "Test" and its actually setting text to "Test" instead of "0" now. But still that doesn't explain to me, why script gets triggered by Shift when window not active.
 

TailBit

Member
You could replace show_debug_message("6"); with show_debug_message(keyboard_lastkey); just for the extra information.

..could be written like this instead of looping through all instances in the room:
GML:
/// @description Remove Duplicate
with(obj_Controls_Slot)
{
    if key_code == keyboard_lastkey
    {
        key_code = 0;
        key_string = "Not Set";
        sprite_index = spr_Controls_SlotEmpty;
    }
}
 
You could replace show_debug_message("6"); with show_debug_message(keyboard_lastkey); just for the extra information.
Yea, I've done that before too, prints "0" 7 times and with show_debug_message(keyboard_lastchar) as well, prints empty space 7 times
 

TailBit

Member
if you press the "table" button there, then it says that 0 = "that key has no keycode" .. which is strange

So you might just want to stop the whole check if the keycode is anything less then 3
 
if you press the "table" button there, then it says that 0 = "that key has no keycode" .. which is strange

So you might just want to stop the whole check if the keycode is anything less then 3
Yea, very strange. Thanks for advise!
If anyone else have ideas what is going on with that Shift, I am open for discussion.
 

kburkhart84

Firehammer Games
So you might just want to stop the whole check if the keycode is anything less then 3
In my input system, I actually prioritize an ordering for polling for input. It checks gamepads first(XInput and then DInput since XInput stuff shows up as DInput in a worse version). Then it checks mouse, and finally keyboard. I do the mouse first because I was running into a thing where left clicking was showing up as keycode 7 on the keyboard or something(likely something gamemaker was doing). And then I even prioritize checking the modifier keys before the rest of the keys, because gamemaker actually puts the pairs of them(shift, alt, ctrl) into a single keycode. I check them separately first left/right so that if they are pressed, I pick that up, and never detect the press of the "paired single keycode." The same applies to XInput/DInput, I discover the XInput version first so never pick up the inferior DInput versions.

EDIT****

Forgot to mention, I'm not using the "last key" variable for anything at all. I prefer to directly poll the devices, as this seems like a more stable way to go about it to me.
 

chamaeleon

Member
I have no idea if there's any kind of triggering going on specifically when shift is being pressed, even if the window is not in focus (I can't repeat it anyway). However, keyboard_lastkey does get reset to 0 as soon as the game window loses focus from what I can tell by drawing its value in a draw event (pressing shift while the program is in focus give 16 as the last key). Since keyPressed is set 0 in some places, and the comparison when the reset happens is keyPressed == keyboard_lastkey, my gut reaction is that the effect would be triggered by losing focus more than pressing shift, based on what I have seen on my computer.
 
Top