public override string ToString() { var builder = new StringBuilder(); if (!IgnoreModifiers) { if ((Modifiers & EventModifiers.Command) > 0) { if (builder.Length > 0) { builder.Append('+'); } builder.Append("Command"); } if ((Modifiers & EventModifiers.Control) > 0) { if (builder.Length > 0) { builder.Append('+'); } builder.Append("Control"); } if ((Modifiers & EventModifiers.Alt) > 0) { if (builder.Length > 0) { builder.Append('+'); } builder.Append("Alt"); } if ((Modifiers & EventModifiers.Shift) > 0) { if (builder.Length > 0) { builder.Append('+'); } builder.Append("Shift"); } } if (builder.Length > 0) { builder.Append('+'); } builder.Append(KeyEvent.CodeToString(KeyCode)); if (Hold) { builder.Append(" (hold)"); } return(builder.ToString()); }
static void DoKeyEventField(Rect position, int index, ref KeyCodeWithModifier key, GUIStyle style) { int id = GUIUtility.GetControlID(s_KeyEventFieldHash + index, FocusType.Passive, position); Event evt = Event.current; switch (evt.GetTypeForControl(id)) { case EventType.MouseDown: { // If the mouse is inside the button, we say that we're the hot control if (position.Contains(evt.mousePosition)) { GUIUtility.hotControl = id; evt.Use(); if (bKeyEventActive == id) // cancel { bKeyEventActive = -1; } else { bKeyEventActive = id; } } return; } case EventType.MouseUp: { if (GUIUtility.hotControl == id) { GUIUtility.hotControl = id; // If we got the mousedown, the mouseup is ours as well // (no matter if the click was in the button or not) evt.Use(); } return; } case EventType.MouseDrag: { if (GUIUtility.hotControl == id) { evt.Use(); } break; } case EventType.Repaint: { if (bKeyEventActive == id) { style.Draw(position, defaultKeyContent, id); } else { string str = KeyEvent.CodeToString(key.KeyCode); style.Draw(position, new GUIContent(str), id); } break; } case EventType.KeyDown: { if ((GUIUtility.hotControl == id) && bKeyEventActive == id) { // ignore presses of just modifier keys if (evt.character == '\0') { if (evt.alt && (evt.keyCode == KeyCode.AltGr || evt.keyCode == KeyCode.LeftAlt || evt.keyCode == KeyCode.RightAlt) || evt.control && (evt.keyCode == KeyCode.LeftControl || evt.keyCode == KeyCode.RightControl) || evt.command && (evt.keyCode == KeyCode.LeftApple || evt.keyCode == KeyCode.RightApple || evt.keyCode == KeyCode.LeftWindows || evt.keyCode == KeyCode.RightWindows) || evt.shift && (evt.keyCode == KeyCode.LeftShift || evt.keyCode == KeyCode.RightShift || (int)evt.keyCode == 0)) { return; } } bKeyEventActive = -1; GUI.changed = true; key.KeyCode = evt.keyCode; key.Modifiers = (evt.command ? EventModifiers.Command : 0) | (evt.alt ? EventModifiers.Alt : 0) | (evt.shift ? EventModifiers.Shift : 0) | (evt.control ? EventModifiers.Control : 0); GUIUtility.hotControl = 0; evt.Use(); return; } break; } } }