Blimey. I’ve just realised It’s been just over a year since I signed up for this course…I really need to stop taking breaks and get on with it! Yes, I’m still on text101, but I’ve been learning loads trying to figure out the things I’m trying to put into my more advanced version of this game.
So far, all keystrokes have been registered through getkeydown which is looking for specific keys being pressed.
Is there a similar command that looks for any key being pressed?
As keys are pressed, they are then stored all together in a single string variable.
I’ve tried a bit of Googling but I’m only finding what seem to be overly complicated ways of doing a very simple thing.
Hi Robert,
You could use “Input.anyKey” , that is returning a bool.
Here is a link to the documentation
https://docs.unity3d.com/ScriptReference/Input-anyKey.html
Hope that will help you !
Unless I’m mistaken (which is entirely possible), doesn’t this just check for any key being pressed and if something is pressed, it does the next bit of code?
Can I actually grab this input and save it to a variable?
Oh i misunderstood the question.
I don’t think there is a property or function that can return which key is pressed…
I guess that the only solution that could work is the following :
foreach(KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))) {
if(Input.GetKey(vKey)){
//your code here
}
}
Thanks for that, but I’m unsure how to…utilise this.
The code I currently have is this -
void typing1 () {
text.text = "Please type the password.\n"+
"The screen on the keyboard reads -\n'Password - "+PLAYER_CODE_GOES_HERE_AS_ITS_BEING_TYPED+"\n\n"+
"Type the password and press RETURN to continue";
//other code here to check the password is correct or not
}
That crazy variable name ‘PLAYER_CODE_GOES_HERE_AS_ITS_BEING_TYPED’, I added for your benefit to give you an idea what should be happening. I will then check that variable in the rest of the code to make sure that it’s the right password or not.
How to I marry together your suggestion with what I hoped to happen in my code? Does it even work that way?
Here is a solution. There is 1 remaining problem :
While it’s in the “Update” method, you have to type VERY quickly your letter, otherwise it will spam the same letter in the password variable… Didn’t find THE solution, but this is still one
EDIT : Problem solved by replacing “Input.anyKey” by “Input.anyKeyDown”
I limited the password size to 4, but this is up to you to define the max size !
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Test : MonoBehaviour {
public string password = "";
public Text text;
// Use this for initialization
void Start () {
text.text = password.ToString();
}
// Update is called once per frame
void Update () {
if(Input.anyKeyDown && password.Length < 4) {
UpdatePassword();
}
}
private void UpdatePassword() {
foreach(KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))) {
if(Input.GetKey(vKey)){
password += vKey.ToString();
text.text = "Please type the password.\n"+
"The screen on the keyboard reads -\n'Password - "+password+"\n\n"+
"Type the password and press RETURN to continue";
}
}
}
}