- Home /
Issues with Input.inputString
Hey folks!
I have some issues when using Input.inputString – when I am getting an user input (Input.anyKeyDown) and Debug.Log the Input.inputString, it works perfectly fine with the standard keys like a–z or 1–0. But using the ESC and arrow keys will result either in an empty string or jibberish.
I am using Unity 2018.1.1 on a MacBook Pro with MacOS 10.13.6.
Anybody having the same issues or knows how to fix it? I need the Input.inputString to return exactly the key pressed for individualising the keyboard layout for a player.
Thanks in advance for your reply!
Still haven't figured out what the problem is. Anybody? :)
void Update () {
// ESC, left click, right click, middle click do not trigger
if(Input.any$$anonymous$$eyDown && !(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape) || Input.Get$$anonymous$$ouseButtonDown(0) || Input.Get$$anonymous$$ouseButtonDown(1) || Input.Get$$anonymous$$ouseButtonDown(2))) {
// reset value of other command, if this key is already used
for(int i = 0; i < settings$$anonymous$$anager.keyboardLayout.Length; i++) {
if(settings$$anonymous$$anager.keyboardLayout[i] == Input.inputString.ToUpper()) {
settings$$anonymous$$anager.Set$$anonymous$$ey(i, "");
}
}
settings$$anonymous$$anager.Set$$anonymous$$ey(settings$$anonymous$$anager.current$$anonymous$$ey, Input.inputString.ToUpper());
settings$$anonymous$$anager.SaveSettings();
gameObject.SetActive(false);
menu$$anonymous$$anager.isEntering$$anonymous$$ey = false;
}
}
The function settings$$anonymous$$anager.Set$$anonymous$$ey() just sets the value of settings$$anonymous$$anager.current$$anonymous$$ey (a strig) to Input.inputString.ToUpper(). That is all – for keys lika a–z and 0–1 the values are correct, but, for example, the up arrow key returns a Hindi character.
The //ToUpper()// is not what makes a wrong return, since it also returns the Hindi character without the method.
Answer by Estecka · Jul 31, 2018 at 03:30 PM
Input.inputString
is not designed to return the pressed key, (otherwise it would be returning a keycode), it's designed to return the text that the pressed keys would write into a text field. Escape and the arrow keys don't write anything so the inputstring is empty, the inputstring for Return should be "\n", etc...
In order to achieve you seek, I think you need to look into the Event
class instead of Input
. The snippet of code on this page seems to do just that :
https://docs.unity3d.com/ScriptReference/Event-keyCode.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnGUI() {
Event e = Event.current;
if (e.isKey)
Debug.Log("Detected key code: " + e.keyCode);
}
}
Perfect! This is exactly what I was looking for :) Thank you very much!
Answer by brendanperry05 · Jul 31, 2018 at 03:07 PM
According to the docs Input.inputString can be used for ASCII characters and the backspace and return keys only.
Oh, I see – I must have somehow skipped this in the documentation. Thanks for the hint!
So, I assume, since there is no other already existing variable by Unity, the best way to detect all other keys would be by putting other if statements within the //Update()// function? It is a pitty that easy things like these are not dealt with automatically :D
I would do some more research on that if I were you but I have never personally made any games that require me to face this issue. If a chain of if statements gets the job done then I would start there and keep your eyes open for improvements. Best of luck!
Your answer
Follow this Question
Related Questions
Press GUI button with key press 1 Answer
Distribute terrain in zones 3 Answers
Checking whether string is a valid Input.Key 1 Answer
Unity 2d select game object with keyboard 2 Answers
Can § be used as input? 3 Answers