// Mallet collision. Check if we've hit a keyboard key or not. void OnTriggerEnter(Collider other) { if (_keyboard == null) { Debug.LogError("Huh, I, this keyboard mallet, have struck something. However, I am not the child of a keyboard. A lost soul. It pains me to ignore this collision event. What does it mean? Who was it meant for? Unfortunately I am given no choice."); return; } Rigidbody keyRigidbody = other.attachedRigidbody; if (keyRigidbody == null) { return; } KeyboardKey key = keyRigidbody.GetComponent <KeyboardKey>(); if (key != null) { if (key.IsMalletHeadInFrontOfKey(this)) { _keyboard._MalletStruckKeyboardKey(this, key); TriggerHapticPulse(); } } else { // Trigger haptic pulse (originally I wanted to limit this to just key strikes, but I guess haptics make sense if you hit anything...) TriggerHapticPulse(); } }
// Internal public void _MalletStruckKeyboardKey(KeyboardMallet mallet, KeyboardKey key) { // Did we hit the key for another keyboard? if (key._keyboard != this) { return; } // Trigger key press animation key.KeyPressed(); // Fire key press event if (keyPressed != null) { string keyPress = key.GetCharacter(); bool shouldFireKeyPressEvent = true; if (keyPress == "\\s") { // Shift shift = !shift; shouldFireKeyPressEvent = false; } else if (keyPress == "\\l") { // Layout swap if (layout == Layout.Letters) { layout = Layout.Numbers; } else if (layout == Layout.Numbers) { layout = Layout.Letters; } shouldFireKeyPressEvent = false; } else if (keyPress == "\\b") { // Backspace keyPress = "\b"; } else { // Turn off shift after typing a letter if (shift && layout == Layout.Letters) { shift = false; } } if (shouldFireKeyPressEvent) { keyPressed(this, keyPress); } } }
// Internal public void _MalletStruckKeyboardKey(KeyboardMallet mallet, KeyboardKey key) { // Did we hit the key for another keyboard? if (key._keyboard != this) { return; } GameObject grandparent = key.transform.parent.gameObject.transform.parent.gameObject; bool isLetter = grandparent == _letters; if (isLetter) { // Gesture Mode: Don't trigger the same key twice if (key == prevKey) { return; } prevKey = key; // log keys KeySequence += key.character; } // Trigger key press animation key.KeyPressed(); // Fire key press event if (keyPressed != null) { string keyPress = key.GetCharacter(); bool shouldFireKeyPressEvent = true; if (keyPress == "\\s") { // Shift shift = !shift; shouldFireKeyPressEvent = false; } else if (keyPress == "\\l") { // Layout swap if (layout == Layout.Letters) { layout = Layout.Numbers; } else if (layout == Layout.Numbers) { layout = Layout.Letters; } shouldFireKeyPressEvent = false; } else if (keyPress == "\\b") { // Backspace keyPress = "\b"; } else { // Turn off shift after typing a letter if (shift && layout == Layout.Letters) { shift = false; } } if (shouldFireKeyPressEvent) { keyPressed(this, keyPress); } } }