Beispiel #1
0
        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;
            }
            }
        }
 public KeyDescriptionAttribute(string name, KeyCode code, EventModifiers modifier, KeyOptions options = KeyOptions.CreateMenu)
 {
     var keys = new KeyCodeWithModifier[1] {
         new KeyCodeWithModifier(code, modifier, options)
     }; KeyEvent = new KeyEvent(name, keys);
 }