Is there a smarter way of handling Keyboard input than a bunch of if/elses?

Currently, we handle Keyboard input as follows:

void ProcessKey(KeyCode k)
{
	if (k == KeyCode.Q)
	{
		// Do stuff
	}

	if (k == KeyCode.W)
	{
		// Do stuff
	}

	if (k == KeyCode.E)
	{
		// Do stuff
	}

	if (k == KeyCode.R)
	{
		// Do stuff
	}
	
	// ...
}

Is there a smarter way of doing this? It seems like this method will get very long and unmanageable.

I realize I can separate different “Do Stuffs” into things like “HandleQ()”, “HandleW()”, “HandleE()” methods, and so forth, but even still I’m not totally satisfied.

You can set up values in the Input Manager and then use Input.GetAxis to get the input value.

Add your axis names to a list of objects that contain the axis name and a delegate for handling the input. Then just loop through the list each frame, call GetAxis passing in the axis name, call the delegate and pass in the input value.