forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventModifiers.cs
More file actions
111 lines (96 loc) · 3.93 KB
/
EventModifiers.cs
File metadata and controls
111 lines (96 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Bindings;
namespace UnityEngine.InputForUI
{
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal struct EventModifiers
{
// Types of modifier key that can be active during a keystroke event.
[Flags]
public enum Modifiers : uint
{
LeftShift = 1u << 0,
RightShift = 1u << 1,
Shift = LeftShift | RightShift,
LeftCtrl = 1u << 2,
RightCtrl = 1u << 3,
Ctrl = LeftCtrl | RightCtrl,
LeftAlt = 1u << 4,
// Generally try to avoid using RightAlt for anything hardcoded due to it being the same as AltGr key.
RightAlt = 1u << 5,
Alt = LeftAlt | RightAlt,
// If Windows or Command key is pressed.
LeftMeta = 1u << 6,
RightMeta = 1u << 7,
Meta = LeftMeta | RightMeta,
CapsLock = 1u << 8,
Numlock = 1u << 9,
// TODO For some reason IMGUI also has a flag if any F1-F15 keys are pressed + extra keys.
// TODO Not sure what it is about or how it is used exactly.
// TODO https://github.cds.internal.unity3d.com/unity/unity/blob/trunk/Modules/IMGUI/Event.cs#L228-L256
FunctionKey = 1u << 10,
Numeric = 1u << 11
}
private uint _state;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsPressed(Modifiers mod) => (_state & (uint)mod) != 0;
public bool isShiftPressed => IsPressed(Modifiers.Shift);
public bool isCtrlPressed => IsPressed(Modifiers.Ctrl);
public bool isAltPressed => IsPressed(Modifiers.Alt);
public bool isMetaPressed => IsPressed(Modifiers.Meta);
public bool isCapsLockEnabled => IsPressed(Modifiers.CapsLock);
public bool isNumLockEnabled => IsPressed(Modifiers.Numlock);
public bool isFunctionKeyPressed => IsPressed(Modifiers.FunctionKey);
public bool isNumericPressed => IsPressed(Modifiers.Numeric);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetPressed(Modifiers modifier, bool pressed)
{
if (pressed)
_state |= (uint)modifier;
else
_state &= ~(uint)modifier;
}
public void Reset()
{
_state = 0;
}
private static void Append(ref string str, string value)
{
str = string.IsNullOrEmpty(str) ? value : $"{str},{value}";
}
public override string ToString()
{
var str = string.Empty;
if (IsPressed(Modifiers.LeftShift))
Append(ref str, "LeftShift");
if (IsPressed(Modifiers.RightShift))
Append(ref str, "RightShift");
if (IsPressed(Modifiers.LeftCtrl))
Append(ref str, "LeftCtrl");
if (IsPressed(Modifiers.RightCtrl))
Append(ref str, "RightCtrl");
if (IsPressed(Modifiers.LeftAlt))
Append(ref str, "LeftAlt");
if (IsPressed(Modifiers.RightAlt))
Append(ref str, "RightAlt");
if (IsPressed(Modifiers.LeftMeta))
Append(ref str, "LeftMeta");
if (IsPressed(Modifiers.RightMeta))
Append(ref str, "RightMeta");
if (IsPressed(Modifiers.CapsLock))
Append(ref str, "CapsLock");
if (IsPressed(Modifiers.Numlock))
Append(ref str, "Numlock");
if (IsPressed(Modifiers.FunctionKey))
Append(ref str, "FunctionKey");
if (IsPressed(Modifiers.Numeric))
Append(ref str, "Numeric");
return str;
}
}
}