Welcome, Guest

Author Topic: Unity3D and sending shift key  (Read 1864 times)

John

  • Coherent Labs support
  • Administrator
  • *****
  • Posts: 205
    • View Profile
Unity3D and sending shift key
« on: December 15, 2015, 07:05:57 AM »
Hello,

The problem is that Unity3D is not sending an event for the Shift keys. It is an old and relatively well known problem, as you can see in this topic from 2009.

Our developers have made a workaround, if you need it. You can put the following code inside void LateUpdate() in CoherentUISystem.cs:

Code: [Select]
		#region Shift key press workaround
bool isAnyKeyDownExceptShift = false;
foreach (KeyCode i in System.Enum.GetValues(typeof(KeyCode)))
{
if (i != KeyCode.LeftShift && i != KeyCode.RightShift && Input.GetKey (i)) {
isAnyKeyDownExceptShift = true;
break;
}
}
foreach (var item in m_Views) {
var view = item.View;
if (!isAnyKeyDownExceptShift &&
    (Input.GetKeyDown (KeyCode.LeftShift) || Input.GetKeyDown (KeyCode.RightShift))) {
Coherent.UI.KeyEventData keyEventData = new KeyEventData ();
keyEventData.Modifiers = new EventModifiersState ();
keyEventData.Modifiers.IsShiftDown = true;
keyEventData.Type = KeyEventData.EventType.KeyDown;
if (Input.GetKeyDown (KeyCode.LeftShift)) {
keyEventData.KeyCode = 160;
} else {
keyEventData.KeyCode = 161;
}
view.KeyEvent (keyEventData);
}
if (Input.GetKeyUp (KeyCode.LeftShift) || Input.GetKeyUp (KeyCode.RightShift)) {
Coherent.UI.KeyEventData keyEventData = new KeyEventData ();
keyEventData.Modifiers = new EventModifiersState ();
keyEventData.Type = KeyEventData.EventType.KeyUp;
if (Input.GetKeyUp (KeyCode.LeftShift)) {
keyEventData.KeyCode = 160;
} else {
keyEventData.KeyCode = 161;
}
view.KeyEvent (keyEventData);
}
}
#endregion


We recommend using this, if you MUST send the shift key as a standalone event, as it comes with a small performance cost. There aren't any problems with keypresses and mouse clicks while it is pressed.

Regards,
John
« Last Edit: January 12, 2016, 09:36:38 AM by John »

Tags: