- Home /
C# Creating Hotkeys
Hi everyone, I'm trying to make a hotkey script that allows me to assign hotkeys. But I'm not sure how. How do you figure out which key was assigned as a hotkey and how do create an if statement that checks whether or not that key was pressed?
Answer by randomuser · Jun 03, 2013 at 12:50 AM
First, make sure you extend from editor and not monobehavior.
Second, here is a example on exactally on what you are talking about. http://wiki.unity3d.com/index.php/Blender_Camera_Controls
If you still need help after looking at this example then send a comment and I will explain it in more detail.
I was think more on the lines of Diablo 2 hotkeys. This is just one switch statement. I want to actually assign which hotkeys I can use.
You're talking about something in-game, right? So an editor script is useless to you. The switch statement is spot-on, though- all you need to do is put that in 'OnGUI' and switch over 'Event.current.keyCode'. If you wanted them to be customisable, you could easily ask the player to press a key, then record the next pressed keyCode as the new hotkey.
There are custom input managers on the Asset Store, most of which will handle hotkeying for you. I probably wrote one of them :)
It's easiest to store keys as $$anonymous$$eyCode values:
if ( listeningForInput && Event.current.keyCode != $$anonymous$$eyCode.None ) {
listeningHot$$anonymous$$ey = Event.current.keyCode;
}
Yes but how do I detect which key was pressed? With a for loop maybe?
You're not listening. In 'OnGUI', you can do this:
if(Event.current.is$$anonymous$$ey) {
key = Event.current.keyCode;
}
In case it wasn't clear, almost all of the Unity editor was implemented with the very GUI functions you have available to you in-game- which means that most things they can do, you can do.
Your answer

Follow this Question
Related Questions
C# How to Detect Edges of a Collider 0 Answers
FPS keep a loadout 0 Answers
C# Number Key For Loop 1 Answer
C# GUI.Tooltip If Statement 2 Answers
C# SetActive GameObject Array 2 Answers