• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

iOS How to check if the return key has been pressed?

Pfap

Member
I have been playing around with the new virtual keyboard functions with ios and have noticed a few issues.

I have an object that brings up the keyboard in the first room of my tests. It wasn't bringing the keyboard up, but there was some strange flicker in the default Gms2 splash screen, so I put an alarm in the objects create event and had the alarm bring the keyboard up and that worked fine.

The main reason I made this post and I hope somebody has a solution is that the keyboard events don't seem to work with the virtual keyboard. I am specifically trying to check for when the user presses the return key and have tried the following.

I checked the manual and I am not using predictive text.
The last argument is to enable/disable predictive text, and this would be set to true to permit it, and false otherwise, but note that just because it is permitted doesn't mean that it will be used as that will depend on the preferences of the user on the device. When in predictive text mode, the virtual keyboard will not generate normal GameMaker Studio 2 keypress events. Instead, it will only update the last character pressed and keyboard string variables. This is due to the inability to detect whether a change in the internal text field used for detecting key presses came from an actual virtual keyboard key, or a text suggestion. In these cases you would want to read the keyboard_string input as opposed to reading any kind of raw key input.


I tried checking keyboard_key to find the keycode and the built in variable keyboard_key does not work on ios.
I figured the return code would be 13 and a quick web search does confirm this. The key press enter event does not work either. I also tried with the step event.







Code:
current_key = keyboard_key;


//13 should be the return key and if the string is empty don't do anything
if current_key == 13 && my_string != "" && click_once == true{
 
 

 show_debug_message("The return key has been pressed");
 click_once = false;

 
}


Any ideas?
 

Mick

Member
You should be able to check the last char of keyboard_string. If it's a new line character (\n), then return has been pressed. You should be able to clear keyboard_string manually once you have processed it, or just erase the new line character.

I haven't tried the built in virtual keyboard myself, so there might be a better way.
 

Pfap

Member
You should be able to check the last char of keyboard_string. If it's a new line character (\n), then return has been pressed. You should be able to clear keyboard_string manually once you have processed it, or just erase the new line character.

I haven't tried the built in virtual keyboard myself, so there might be a better way.
You should be able to check the last char of keyboard_string. If it's a new line character (\n), then return has been pressed. You should be able to clear keyboard_string manually once you have processed it, or just erase the new line character.

I haven't tried the built in virtual keyboard myself, so there might be a better way.

I got it working on the pc using (\r) I will post to this thread once I have tested on ios.
 

Pfap

Member
I have the same problem, tried vk_enter and vk_return
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:
This variable holds a string containing the last (at most) 1024 characters typed on the keyboard. This string will only contain printable characters typed, but it will correctly respond to pressing the backspace key by erasing the last character. This variable is not read only and you can change it, for example to set it to "" (an empty string) if you handled it already, or use the String Functions to manipulate it.

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.
 
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.
Good findings, but this is not good practice by yoyogames part. They just need to make a function called keyboard_virtual_return_pressed or map to vk_return, with proper documentation ofcourse. I don't see the reasoning behind setting rk_ in virtual keyboard without this.

Edit: Pfap, what if a user presses Shift, Alt, Ctrl etc?
 
Last edited:

Pfap

Member
Good findings, but this is not good practice by yoyogames part. They just need to make a function called keyboard_virtual_return_pressed or map to vk_return, with proper documentation ofcourse. I don't see the reasoning behind setting rk_ in virtual keyboard without this.

Edit: Pfap, what if a user presses Shift, Alt, Ctrl etc?

I'm not sure, which is kind of why I had this comment

Code:
if keyboard_check_pressed(vk_anykey){
//if hold_string5 == hold_string6 the strings are the same length and the player is probably tapping send

Edit:
You could check if the other keyboard constants work. If it's only vk_enter that doesn't you could make statements that will only run if hold_string5 == hold_string6 && !vk_shift.
 
Top