• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

GML [SOLVED] Convert string to keyboard input or return keyboard input?

XirmiX

Member
Is it possible to convert a string to a keyboard input? Say I have "mouse_x" string variable and I want to create a new variable that holds keyboard input and takes that string variable to get what key the variable will be set to?

Or may be it's possible to set in a list a key value? I've tried it, and I only get numbers instead, unless I write them as strings, so that's really why I'm asking.
 

XirmiX

Member
I realise that mouse_x isn't a key of any sorts, but the cursor position axis, which explains why I got an integer stored in the ds_list for that. But what about something like mb_left?

@TheouAegis but if they are integers, how am I supposed to be able to tell what key the value holds? I need to be able to either store button names like mb_left or vk_space in a ds_list so that they could be read from as buttons or turned into button names and not strings or integers. And I can't have something like:

Code:
if (ds_list_find_value(button_list, 0) == "mb_left"){
    button_var = mb_left;
}
The code needs to be flexible for any sort of button type, and having that for all types of buttons wouldn't be ideal.
 

FrostyCat

Member
You simply insert them into variables or data structures and read them back like you would any other value. Just DON'T add quotes.
Code:
ds_list_add(button_list, mb_left);
Code:
button_var = button_list[| 0];
If you don't have qualms about putting true or pi into a variable or data structure, then you shouldn't have qualms about doing the same with keycodes or mouse constants.
 

Tsa05

Member
Code:
code = "Hello";

var pos = 1;
while(pos<=string_length(code)){
    var press = string_upper(string_char_at(code, pos));
    event_perform(ev_keypress, ord(press));
}
You can get GMS to perform any key press event you please, in the manner above.
Alternately, if you wanted keyboard_string to get the value, set it directly.

If you wanted GMS to actually simulate the press at an OS level and make other programs thing keys were pressed, that's no longer in GameMaker, and would need an extension.
 

TheouAegis

Member
Either make a second set of variables that holds values for if the command is keyboard, mouse, or gamepad; or store the same value in the highest bits of eeach variable, then separate the mode bit from the keycode value.

If you leave out gamepads, I'm pretty sure all mouse constants are less than 32 and all keyboard ones used are over 32.
 

XirmiX

Member
@FrostyCat why didn't I think of that, it makes sense. Though it seems like my code was a bit more involved than I thought it would be. The button click built-in variables can be returned to then after be checked, but you can't use a mouse button/keyboard button press check the same way, as from a function you only get a return value, not the execution of it. Not sure if script_execute works with built-in functions like keyboard_check_pressed(), but if it does, that might work for my case.
 
Top