AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ButtonController.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity.InputModule
7 {
8 
13  public class ButtonController : MonoBehaviour
14  {
18  public enum ButtonType
19  {
20  Left,
21  Right,
22  Middle,
23  Control,
24  Shift,
25  Alt,
26  Space,
27  Return,
28  Focused,
29  ControlAndLeft,
30  ControlAndRight,
31  ControlAndMiddle,
32  ShiftAndLeft,
33  ShiftAndRight,
34  ShiftAndMiddle,
35  AltAndLeft,
36  AltAndRight,
37  AltAndMiddle,
38  SpaceAndLeft,
39  SpaceAndRight,
40  SpaceAndMiddle,
41  None
42  }
43 
47  public ButtonType buttonType = ButtonType.None;
48 
49  private bool appHasFocus = true;
50 
51  private void Awake()
52  {
53  // ButtonController is for development only and should not exist--and certainly not be used--in
54  // any non-Editor scenario.
55 #if !UNITY_EDITOR
56  Destroy(this);
57 #else
58  // Workaround for Remote Desktop. Ctrl-mouse, Shift-mouse, and Alt-mouse don't work, so they should be avoided.
59  if (IsRunningUnderRemoteDesktop())
60  {
61  if (this.buttonType == ButtonType.Control)
62  {
63  this.buttonType = ButtonType.Left;
64  Debug.LogWarning("Running under Remote Desktop, so changed ButtonController method to Left mouse button");
65  }
66  if (this.buttonType == ButtonType.Alt)
67  {
68  this.buttonType = ButtonType.Right;
69  Debug.LogWarning("Running under Remote Desktop, so changed ButtonController method to Right mouse button");
70  }
71  if (this.buttonType == ButtonType.Shift)
72  {
73  this.buttonType = ButtonType.Middle;
74  Debug.LogWarning("Running under Remote Desktop, so changed ButtonController method to Middle mouse button");
75  }
76  }
77 #endif
78  }
79 
84  public bool Pressed()
85  {
86  bool left = Input.GetMouseButton(0);
87  bool right = Input.GetMouseButton(1);
88  bool middle = Input.GetMouseButton(2);
89  bool control = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
90  bool shift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
91  bool alt = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
92  bool space = Input.GetKey(KeyCode.Space);
93  switch (buttonType)
94  {
95  case ButtonType.Left:
96  return left;
97  case ButtonType.Right:
98  return right;
99  case ButtonType.Middle:
100  return middle;
101  case ButtonType.Control:
102  return control;
103  case ButtonType.Shift:
104  return shift;
105  case ButtonType.Alt:
106  return alt;
107  case ButtonType.Space:
108  return space;
109  case ButtonType.Return:
110  return Input.GetKey(KeyCode.Return);
111  case ButtonType.Focused:
112  return this.appHasFocus;
113  case ButtonType.ControlAndLeft:
114  return control && left;
115  case ButtonType.ControlAndRight:
116  return control && right;
117  case ButtonType.ControlAndMiddle:
118  return control && middle;
119  case ButtonType.ShiftAndLeft:
120  return shift && left;
121  case ButtonType.ShiftAndRight:
122  return shift && right;
123  case ButtonType.ShiftAndMiddle:
124  return shift && middle;
125  case ButtonType.AltAndLeft:
126  return alt && left;
127  case ButtonType.AltAndRight:
128  return alt && right;
129  case ButtonType.AltAndMiddle:
130  return alt && middle;
131  case ButtonType.SpaceAndLeft:
132  return space && left;
133  case ButtonType.SpaceAndRight:
134  return space && right;
135  case ButtonType.SpaceAndMiddle:
136  return space && middle;
137  case ButtonType.None:
138  default:
139  return false;
140  };
141  }
142 
143  private void OnApplicationFocus(bool focusStatus)
144  {
145  this.appHasFocus = focusStatus;
146  }
147 
148 #if UNITY_EDITOR
149  [System.Runtime.InteropServices.DllImport("kernel32.dll")]
150  private static extern uint GetCurrentProcessId();
151 
152  [System.Runtime.InteropServices.DllImport("kernel32.dll")]
153  private static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId);
154 
155  [System.Runtime.InteropServices.DllImport("kernel32.dll")]
156  private static extern uint WTSGetActiveConsoleSessionId();
157 
158  private bool IsRunningUnderRemoteDesktop()
159  {
160  uint processId = GetCurrentProcessId();
161  uint sessionId;
162  return ProcessIdToSessionId(processId, out sessionId) && (sessionId != WTSGetActiveConsoleSessionId());
163  }
164 #else
165  private bool IsRunningUnderRemoteDesktop()
166  {
167  return false;
168  }
169 #endif
170 
171  }
172 
173 } // namespace
ButtonController provides a per key or button component for the Manual input Controls in the Unity Ed...
bool Pressed()
Returns true if the configured button is currently pressed.
ButtonType
These enums allow us to activate an axis only by a key press, such as CTRL mouse or ALT mouse ...
Useful for releasing external override. See CursorStateEnum.Contextual