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

Android Virtual Keyboard

Discussion in 'GameMaker Studio 2 Community Tech Support' started by Bacon&Onions, Aug 3, 2018.

  1. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    Has anyone had sucess with the new Virtual keyboard? I've tried pulling it out on android but it doesn't work.
    I called keyboard_virtual_show(kbv_type_default,kbv_returnkey_default,kbv_autocapitalize_none,0)
    and it shows up just fine but game maker doesn't respond to the text input.
    Here's what I'm using in the step -

    Code:
    /// @description Step event Typing
    var k=keyboard_lastkey;
    var c=keyboard_lastchar;
    
        if keyboard_check_pressed(vk_anykey)
        {
            //if sbore>=compare
            //global.Name += chr(keyboard_key);
        
             if (k==vk_backspace)
            {
                // delete last character
                global.Name = string_copy(global.Name, 1, string_length(global.Name)-1 );
            }
            else if (k==vk_enter) and string_length(global.Name)>=3//and (global.Name!="")
            {
                // enter was pressed and input wasn't empty : do what have to be done here !
                instance_create_depth(x,y,1,oHighscore)
                if global.mobile keyboard_virtual_hide()
                instance_destroy()
                
            }
            else if k>=65 and k<=90 or k>=97 and k<=122 or k>=48 and k<=57 or k==192 or k==32
            if  string_length(global.Name)<=stringmax
            {
                // 65-90  : upcase letters
                // 122-192 : lowercase letters
                // 48-57  : numbers
                // 192   : I don't remember :P
                // 32   : space
                // Add c to name
                global.Name = global.Name + c;
            } else blink=" "
    
        }
    
    
     
    MarceloP likes this.
  2. flykidsblue1

    flykidsblue1 Member

    Joined:
    Jul 6, 2017
    Posts:
    39
    No luck on my side either, I have tried to make it work but with no luck so far. I read the documentation and saw that pressing the letters, closing and opening the keyboard called some asynchronous events. I don't know if we need to use the asynchronous events.
     
  3. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,968
    Okay, there is a slight error in the docs and all input from the virtual keyboard is ONLY captured in the "keyboard_string" variable. Change your code to use that and is should all work... sorry about that and I'll get the docs fixed for the next updated.
     
    Bacon&Onions and MarceloP like this.
  4. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,305
    Is keyboard_string cleared on keyboard_virtual_show(), or need to be done manually?
     
  5. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    Thanks for the clarification! keyboard_string works but I'm going to need more help with this. I can't get the enter button on the virtual keyboard to respond.
    Code:
    if global.mobile
            {global.Name = keyboard_string 
            if [enter command] and string_length(global.Name)>=3)
                { do stuff }
    
    Neither keyboard_key_press(vk_enter) or keyboard_lastkey=vk_enter work.
     
  6. Mick

    Mick Member

    Joined:
    Jun 30, 2016
    Posts:
    726
    You should be able to check if last character of keyboard_string is '\r', according to this post: https://forum.yoyogames.com/index.p...eturn-key-has-been-pressed.51340/#post-314687
     
  7. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    Thanks Mick but I have no idea how to do that.
    keyboard_key command doesn't work with the virtual keyboard.
     
  8. Mick

    Mick Member

    Joined:
    Jun 30, 2016
    Posts:
    726
    Something like:
    Code:
    if( string_char_at( keyboard_string, string_length(keyboard_string) ) == "\r" ) {
      // Do stuff
    }
     
    Nocturne likes this.
  9. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    Thanks Mick, Just tested that on my android phone, and PC. It doesn't work on android or windows. Neither will reconize that.
     
  10. Mick

    Mick Member

    Joined:
    Jun 30, 2016
    Posts:
    726
    Oh, how about "\n" then?
     
  11. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    That doesn't work either. Please test these things out on your own Computer, if we got back and forth like this it will make the thread unreadable.
     
  12. Mick

    Mick Member

    Joined:
    Jun 30, 2016
    Posts:
    726
    @Pfap might be able to help since he seems to have got it working. I don't have the time (nor will) to set up a project to test this at the moment.
     
    Pfap likes this.
  13. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,612
    Maybe he's posting on his phone to try and help you, or doesn't have time to test them himself?
    Considering you made the thread asking for help maybe you shouldn't be so dismissive of someone offering suggestions which in no way make the thread unreadable.
     
  14. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    I apologise. I didn't mean to be rude. You are right I am the one who asked. I have been testing all this stuff out, in addition to my own ideas, and while I'm happy for the help, I am afraid that if we just continue to go back and forth like this, it will be less likely someone will actually come in who will read through all this and help.
    Mick if you or anyone else haves any other ideas you want to try I'll test them on android and PC.
     
    Nocturne likes this.
  15. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    So current status- I got the virtual keyboard working, but the submit button in the bottom right corner of the keyboard remains an elusive mystery.

    If you want to have a virtual keyboard in your mobile game
    Call keyboard_virtual_show(kbv_type_default,kbv_returnkey_default,kbv_autocapitalize_none,0) to bring it up
    in the step event you can put some pretty simple code-
    Code:
    if keyboard_check_pressed(vk_anykey)
        {                  
             if string_length(global.Name)>stringmax  {keyboard_string  = string_copy(global.Name, 1, stringmax) };
             global.Name = keyboard_string;
        }
    
    and then in the draw event draw global.Name
     
  16. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551
    I still have to try pushing it to xcode and I will probably get into Android in the coming weeks. But the \r does work for me on PC tests.

    Code:
    
    
    current_key = keyboard_lastchar;
    //show_debug_message(current_key);
    /*
    maybe manually grab the last character of the keyboard_string by using string_length
    and string copy? if it is "\r" it means the player pressed the return key 
    do the above if the keyboard_lastchar does not work on ios 
    */
    
    //13 should be the return key code and if the string is empty don't do anything
    if current_key == "\r"{
    
    //do stuff here you want to happen after the player has pressed enter
    
    }
    
    
    
    The above code needs to be in the step event; at least according to my tests as the keyboard events do not seem to work on mobile at least not ios… I need to test more.
     
  17. Bacon&Onions

    Bacon&Onions Member

    Joined:
    Aug 2, 2017
    Posts:
    43
    As nocturne said current_key is not used in the virtual keyboard. You won't be able to use it to detect return.
    You have to use keyboard_string to capture all virtual keyboard input
     
  18. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551
    Hmm.... hold on one second I'm going to push it to xcode. And current_key is my own variable I'm using it to store the built in keyboard variable keyboard_lastchar, but now I'm second guessing myself.
     
  19. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551
    Ok, I did a bunch of tests on iOS and it seems that there is no way to directly check for the "return" (enter) key being pressed; although, I found an effective work around.

    From the keyboard_string manual section:
    The reason "\n" works for space and "\r" works for enter on PC is because I could use the keyboard_lastchar which works with those escape characters.
    So, when you tap enter it does not affect the keyboard_string at all. If keyboard_string == "User" and you hit enter keyboard_string == "User".

    On iOS you can check for vk_any, but not vk_enter:

    Code:
    
    
    
    //step event
    
    
    
    
    if keyboard_check_pressed(vk_anykey){
     //if hold_string5 == hold_string6 the strings are the same length and the player is probably tapping send
     switch(which_user_event){
     
      //case 5 will run first
      case 5:
     
      hold_string5 = keyboard_string;
      //alarm[5] = 30;
      event_user(5);
      showing = true;
      //reset the alarms so that the blinking resumes if not pressing
      alarm[0] = 30;
      alarm[1] = 0;
     
      break;
     
      case 6:
     
      hold_string6 = keyboard_string;
      //alarm[6] = 30;
      event_user(6);
      showing = true;
      //reset the alarms so that the blinking resumes if not pressing
      alarm[0] = 30;
      alarm[1] = 0;
     
      break;
     }
    }
    
    
    

    The "return" key is triggered by vk_any.


    Basically just check every other key pressed and if hold_string6 == hold_string5 you know the user tapped enter or return.
     
    Bacon&Onions likes this.
  20. FredFredrickson

    FredFredrickson Member GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    118
    While this workaround works, it's not a good solution to anything put through to production. We really need to have the various enter/return/done/etc. keys from the virtual keyboard map to vk_enter.
     
    flykidsblue1 and Bacon&Onions like this.
  21. flykidsblue1

    flykidsblue1 Member

    Joined:
    Jul 6, 2017
    Posts:
    39
    Worse part is that it's been two or three months since they've added the virtual keyboard functions. I can't believe Yoyo games hasn't done anything to fix this, smh...
     
  22. flykidsblue1

    flykidsblue1 Member

    Joined:
    Jul 6, 2017
    Posts:
    39

    I need help implementing this. I don't quite get how the switch statement would work or any of that. I just wish yoyo did something to fix this...
     
  23. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551

    This code checks the length of the keyboard_string every step and if it has not changed then it assumes the user tapped return. I am thinking there might be a way to have this functionality contained in just the step event, without the user events. The switch statement is just my way of having something fire every other time.


    Create event:
    Code:
    
    
    which_event = 0;
    
    
    hold_string = "";
    hold_string1 = "";
    
    
    Step event:
    Code:
    
    if keyboard_check_pressed(vk_anykey){
    
    //the code in this block will only fire once per step if the user has pressed any key
    
    
     //if hold_string5 == hold_string6 the strings are the same length and the player is probably tapping send. This will be checked in the corresponding user events
    
     switch(which_event){
     
      case 0:
     
      hold_string = keyboard_string;
     
      event_user(0);
     
      break;
     
      case 1:
     
      hold_string1 = keyboard_string;
     
     
      event_user(6);
    
     
      break;
     }
    }
    
    

    User event 0
    Code:
    
    //next step case 1 will fire
    which_alarm = 1;
    
    
    
    show_debug_message(string_length(hold_string));
    show_debug_message(string_length(hold_string1));
    
    //if the strings are the same length it means nothing was added so assume they hit enter
    if string_length(hold_string) == string_length(hold_string1){
    
    //put any code that you would normal put in an enter event or run what you need to after the user hits return
    
    }
    
    

    User event 1
    Code:
    [/COLOR][/FONT][/LEFT][/COLOR][/FONT][/LEFT]
    [FONT=arial][COLOR=rgb(222, 222, 222)]
    [LEFT][FONT=arial][COLOR=rgb(222, 222, 222)]
    [LEFT]
    //next step case 0 will fire
    which_alarm = 0;
    
    
    
    show_debug_message(string_length(hold_string));
    show_debug_message(string_length(hold_string1));
    
    //if the strings are the same length it means nothing was added so assume they hit enter
    if string_length(hold_string) == string_length(hold_string1){
    
    //put any code that you would normal put in an enter event or run what you need to after the user hits return
    
    }
    
    




    The above should be able to be copied and pasted and work.

    Just remove this stuff
    [/COLOR][/FONT][/LEFT]


    Not sure how to edit it out.


    I am not the best teacher and I have the tendency to drone on, so if something is not clear please mention it specifically.
     
    Last edited: Oct 15, 2018
  24. flykidsblue1

    flykidsblue1 Member

    Joined:
    Jul 6, 2017
    Posts:
    39
    That is perfect thank you! From there I can keep working on this! Thanks! Anyways, I was checking for Android, and I saw that simply putting keyboard_check(10) does the trick to close the keyboard. I guess that is not working on apple devices right?
     
  25. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    551
    Glad I could help. You should be able to just call the function keyboard_virtual_hide();.
     
  26. Zhanghua

    Zhanghua Member

    Joined:
    Aug 3, 2017
    Posts:
    170
  27. flykidsblue1

    flykidsblue1 Member

    Joined:
    Jul 6, 2017
    Posts:
    39
    Hey guys, question: is there a way for the keyboard_string to add a "\n"? I'm trying to make the return key add a newline everytime it's pressed. However, it doesn't work the way I though it would(it's not working). Does yoyo not check before adding new functionality to the IDE? They also haven't fixed the keyboard_string resetting every step when the virtual keyboard is not active.
     
  28. FredFredrickson

    FredFredrickson Member GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    118
    Not an answer to your question, flykidsblue1, but after posting here last time, I found this thread, and it solved my issue.
     

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