Custom Controls

I am trying to set up a control customization dialogue, but I don’t know how to capture the user’s keypress.
Here is what I have for this so far:

if(_displayKeyChoice1 == true) {

         GUI.Label(new Rect(200,            // x
                   150,           // y
                   100,           // width
                   buttonHeight),      // height
                   "Press any Key");

         if(Input.anyKeyDown) {

          // Insert what here?

          _displayKeyChoice1 = false;
         }
       }

How do I grab the user’s keypress so I can assign it to the controls in my InputManager?
Also, can you tell me how to make GUI windows opaque rather than transparent?

Anyone?

might be better to capture keys in an Update() instead on OnGUI, using an if(){…} block should work fine, not sure what else you need there.

As always, look at the docs and Unity manual for examples:

Transparent GUI? Just use transparent texture in your GUI Skin.

I need to take the actual key that was pressed and assign it to the custom control in a script that I have set up.
I do not know how to read which key was pressed.

Is there something you could use in Event.current?

I tried using that and can’t figure out if it will help or not.
I have it set up like this:

if(_displayKeyChoice1 == true) {
			
			GUI.Label(new Rect(200,					// x
							150,					// y
							100,					// width
							_buttonHeight),			// height
							"Press any Key");
			
			if(Input.anyKey) {
				
				Event e = Event.current;
				
        		if (e.isKey) {
					
					Debug.Log("Detected character: " + e.keyCode);					
				}
				
				_displayKeyChoice1 = false;
			}
		}

But the debug log does not output anything even though the code appears to behave as it should.

Read the docs on Input.

Input should only be used during Update. It definitely doesn’t work during OnGUI.

You need to use Event during OnGUI:

					if ( Event.current.type == EventType.KeyDown ) {
						if ( Event.current.keyCode != KeyCode.None ) {
							gotKey = true;
							list[ix] = Event.current.keyCode;
						}

Thank you, I will try that…

  • EDIT *

Ya, that fixed me right up. Thank you VERY much. :slight_smile:

void Update()
{
    foreach(KeyCode kc in System.Enum.GetValues(typeof(KeyCode))
    {
	    if(Input.GetKeyDown(kc))
		     Debug.Log("User pressed : " + kc.ToString());
    }
}

The solution I used when coding our input manager was to first setup a GUI-interface with the inputs you want to control.

GUILayout.Label("Forward");
if(GUILayout.Button("W"))
{ /* Save which input you want to change */ }

Then further down the OnGUI I had a bool controlling the reading of input.

if(changeKey)
{
  Event events = Event.current;
  if(events.isKey)
  { /* Store key in array of dictionary */ }
}

Then I stored all the keys in a dictionary with the dictionary key being an enum list of all the keys I used in the game.
So whenever you wanted to know the key later on in game you just typed
dictionaryOfKeys[NameOfEnum.Forward] - Which returned the keycode forward is using.

Hope this helps :slight_smile:

Thanks everyone… I was implementing Louis’s solution as you were posting and it worked just fine.
Have a great day and happy coding.