/// <summary> /// Interface implementation for a callback when the global layout state or /// the visibility of views within the view tree changes. <see cref="ViewTreeObserver.IOnGlobalLayoutListener"/> /// </summary> public void OnGlobalLayout() { lastKeyboardHeight = CalculateKeyboardHeight(); var nextState = CalculateKeyboardsNextState(_lastState, lastKeyboardHeight, InputMethodManager.IsAcceptingText); if (_lastState == nextState) { return; } _lastState = nextState; if (_lastState == KeyboardEventTypes.DidHide) { _keyboardStateChanged?.Invoke(this, new KeyboardStateEventArgs { EventType = KeyboardEventTypes.WillHide, KeyboardHeight = lastKeyboardHeight }); } if (_lastState == KeyboardEventTypes.DidShow) { _keyboardStateChanged?.Invoke(this, new KeyboardStateEventArgs { EventType = KeyboardEventTypes.WillShow, KeyboardHeight = lastKeyboardHeight }); } _keyboardStateChanged?.Invoke(this, new KeyboardStateEventArgs { EventType = nextState, KeyboardHeight = lastKeyboardHeight }); }
private void NotifyKeyboardState(KeyboardEventTypes eventType, UIKeyboardEventArgs eventArgs) { _lastState = eventType; if (eventType == KeyboardEventTypes.DidHide) { _keyboardHeight = 0; } else { _keyboardHeight = CalculateKeyboardHeight(eventArgs); } KeyboardStateChanged?.Invoke(this, new KeyboardStateEventArgs { EventType = eventType, KeyboardHeight = _keyboardHeight }); }
private static KeyboardEventTypes CalculateKeyboardsNextState(KeyboardEventTypes currentState, float keyboardHeight, bool isAcceptingText) { var isKeyboardVisible = keyboardHeight > 10; if (!isKeyboardVisible) { return(KeyboardEventTypes.DidHide); } //NOTE: In case the keyboard is visible(at least partially visible) we continue as follows: if (isAcceptingText) { return(KeyboardEventTypes.DidShow); } //NOTE: If the keyboard is not accepting input yet it means it's not reached is final state yet, it is either in a process of hiding or showing it self if (currentState == KeyboardEventTypes.DidShow) { return(KeyboardEventTypes.WillHide); } if (currentState == KeyboardEventTypes.DidHide) { return(KeyboardEventTypes.WillShow); } return(currentState); }
private void KeyPressed(int keyCode, KeyboardEventTypes type) { // Check the repeat-sensitive keys first so that we can // bail early on repeat events and not check them later // Shift -> increase volume if (_shiftKeyCodes.Contains(keyCode)) { if (type == KeyboardEventTypes.Pressed || type == KeyboardEventTypes.Repeated) { _velocity = Math.Min(_velocity + 32, 100); } return; } // Ctrl -> decrease volume if (_ctrlKeyCodes.Contains(keyCode)) { if (type == KeyboardEventTypes.Pressed || type == KeyboardEventTypes.Repeated) { _velocity = Math.Max(_velocity - 32, 1); } return; } if (type == KeyboardEventTypes.Repeated) { return; } var qwertyIndex = Array.IndexOf(_qwertyIndeces, keyCode); // character key -> piano note if (qwertyIndex >= 0) { var keyIndex = _qwertyIndexToKeyIndex[qwertyIndex]; if (type == KeyboardEventTypes.Pressed) { _bus.Publish(new KeyPressed(keyIndex, _velocity)); return; } _bus.Publish(new KeyReleased(keyIndex)); return; } // Space bar -> sustain pedal if (keyCode == 32) { if (type == KeyboardEventTypes.Pressed) { _bus.Publish(new SustainPedalPressed()); return; } _bus.Publish(new SustainPedalReleased()); return; } // Alt -> soft pedal if (_altKeyCodes.Contains(keyCode)) { if (type == KeyboardEventTypes.Pressed) { _bus.Publish(new SoftPedalPressed()); return; } _bus.Publish(new SoftPedalPressed()); return; } // Ctrl -> sostenuto pedal if (_superKeyCodes.Contains(keyCode)) { if (type == KeyboardEventTypes.Pressed) { _bus.Publish(new SostenutoPedalPressed()); return; } _bus.Publish(new SostenutoPedalReleased()); return; } }
// Temporary API until I nail down keyboard input better. // Based on the GLFW key codes public void PushKeyChange(int keyCode, KeyboardEventTypes type) => OnKeyChange?.Invoke(keyCode, type);