1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

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

Discussion in 'Programming' started by XirmiX, Dec 25, 2018.

  1. XirmiX

    XirmiX Member

    Joined:
    Jul 18, 2016
    Posts:
    365
    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.
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,994
    What do you need a string value for, again? Key values are all integers. Even mouse constants are integers.
     
  3. XirmiX

    XirmiX Member

    Joined:
    Jul 18, 2016
    Posts:
    365
    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.
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,602
    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.
     
  5. Tsa05

    Tsa05 Member

    Joined:
    Jun 21, 2016
    Posts:
    560
    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.
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,994
    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.
     
  7. XirmiX

    XirmiX Member

    Joined:
    Jul 18, 2016
    Posts:
    365
    @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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice