"event.current.keyCode" not work!
I want create menu where player can change default setting for keys (move, jump, shoot etc.). Or I did it wrong, or it doesn't work. If you can, please, help me. In this code I try to listen for pressing key and detect what key was pressed, but it doesn't work, program say, that there are no pressed keys.
public class OnButtonLayoutClick : MonoBehaviour {
public Button button;
bool pressed=false;
void Awake(){
button=(Button)gameObject.GetComponent<Button>() as Button;
button.onClick.AddListener (() => OnMouseDown ());
}
private void OnMouseDown(){
if (!pressed) {
StartCoroutine ("waitForButton");
pressed = !pressed;
} else {
StopCoroutine ("waitForButton");
pressed = !pressed;
}
}
IEnumerator waitForButton(){
string keyCode="";
while (true) {
try{
Event e=Event.current;
if(e.isKey){
Debug.Log(e.keyCode);
}
}
catch(NullReferenceException e){
Debug.Log(e.toString());
}
yield return null;
if (keyCode!=null) StopCoroutine("waitForButton");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Event.KeyCode not detecting mouse click 0 Answers
I am getting an error : ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 0 Answers
error CS0103 rb doesn't exist in the current context, ligne 8 and 15 , can't fix it 1 Answer
How to to fix error UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32) 0 Answers
Error CS1525 0 Answers