/// <summary> /// Init this instance. /// Callbacks are here initialized for the events. /// </summary> private void Init() { SizeChangedCallback = (IntPtr _handle, int width, int height) => { SizeChanged.Invoke(this, new SizeChangedEventArgs { source = this, width = width, height = height }); }; Glfw.SetWindowSizeCallback(this, SizeChangedCallback); KeyPressedCallback = (IntPtr _handle, int key, int scancode, int action, int mods) => { var args = new KeyEventArgs { source = this, key = (Key)System.Enum.Parse(typeof(Key), key.ToString()), action = (State)System.Enum.Parse(typeof(State), action.ToString()), scancode = scancode, mods = mods }; KeyChanged.Invoke(this, args); }; Glfw.SetKeyCallback(this, KeyPressedCallback); // Add dummy handlers to prevent any null reference exceptions SizeChanged = new EventHandler <SizeChangedEventArgs>((__, _) => { }); KeyChanged = new EventHandler <KeyEventArgs>((__, _) => { }); }
private void ReadReport(HidReport hidReport) { var report = new Report(hidReport); if (report.ReadStatus == HidDeviceData.ReadStatus.Success) { if (report.Data.Length == 16) { StringBuilder sb = new StringBuilder(16); for (int i = 0; i < 16; ++i) { bool newState = report.Data[i] != 0; if (_keyState[i] != newState) { KeyChanged?.Invoke(i, newState); } _keyState[i] = newState; sb.Append(_keyState[i] ? '1' : '0'); } Debug.WriteLine($"Keys: {sb.ToString()}"); } } if (_attached && report.ReadStatus != HidDeviceData.ReadStatus.NotConnected) { _hidDevice.ReadReport(ReadReport); } else { _isReading = 0; } }
private bool KeyboardHook_KeyDown(Keys key) { if (hook) { KeyChanged?.Invoke(key); } return(!hook); }
public override void BeforeDispose() { if (KeyChanged != null) { //Remove all Events associated to this control (That haven't been unsubscribed !) foreach (Delegate d in KeyChanged.GetInvocationList()) { KeyChanged -= (EventHandler)d; } } }
private void Gamepad_ButtonChanged(object sender, ButtonEventArgs e) { var rcKey = MapToRemoteControlKey(e.Button, false); if (rcKey != RemoteControlKey.Invalid) { KeyChanged?.Invoke(this, new RemoteControlEventArgs { Key = rcKey, Value = e.Pressed ? 1 : 0 }); } }
private void Gamepad_AxisChanged(object sender, AxisEventArgs e) { var rcKey = MapToRemoteControlKey(e.Axis, true); if (rcKey != RemoteControlKey.Invalid) { var value = ValueMapper.Map(e.Value, -32767, 32767, -1, 1) * ((rcKey == RemoteControlKey.Throttle || rcKey == RemoteControlKey.Yaw) ? -1 : 1); KeyChanged?.Invoke(this, new RemoteControlEventArgs { Key = rcKey, // Gamepad's Throttle and Yaw values are inverted.. fix that before sending Value = value }); } }
// --------------- User Input --------------- private void Button_Click(object sender, RoutedEventArgs e) { Button btn = (sender as Button); if (CurrentlyActivated != btn) { AnimateDeactivate(CurrentlyActivated); } AnimateActivate(btn); Key = GetKey(btn); KeyChanged?.Invoke(this, new KeyChangedEventArgs(Root, Key, btn.Content.ToString(), DiatonicScales[scaleMode], scaleMode)); selectedScaleInMode = true; CurrentlyActivated = (sender as Button); }
protected override void OnVisualModified() { base.OnVisualModified(); // the monitor we shadow may have changed enough to where our key // doesn't match (during reset monitors) // and so we need to reindex in that case and load settings again string newKey = CreateKey(Monitor); if (newKey == Key) { return; } string oldKey = Key; Key = newKey; LoadSettings(); KeyChanged?.Invoke(this, new KeyChangeEventArgs(oldKey, newKey)); }
/// <summary> /// Handle key presses. First read /// </summary> /// <param name="code"></param> /// <param name="wParam">Contains the key event</param> /// <param name="lParam">Contains the key pressed</param> /// <returns></returns> private IntPtr KeyboardHookProc(int code, IntPtr wParam, IntPtr lParam) { if (code >= 0 && (Keys)Marshal.ReadInt32(lParam) == this.Key) { var keyEvent = (KeyEvents)wParam.ToInt32(); if (keyEvent == KeyEvents.KeyDown || keyEvent == KeyEvents.SKeyDown) { if (this.keyState == false || this.Repeat) { this.keyState = true; KeyChanged?.Invoke(null, new KeyChangedEventArgs(this.Key, true)); } } else if (keyEvent == KeyEvents.KeyUp || keyEvent == KeyEvents.SKeyUp) { this.keyState = false; KeyChanged?.Invoke(null, new KeyChangedEventArgs(this.Key, false)); } } return(NativeMethods.CallNextHookEx(IntPtr.Zero, code, wParam, lParam)); }
internal Table(BasicActions <ValueType> BasicActions, Func <ValueType, KeyType> GetKey, KeyType[] XNewKeys, int KeyPos, bool IsUnique, bool IsUpdateAble) { this.GetKey = GetKey; this.BasicActions = BasicActions; this.KeyPos = KeyPos; this.KeysInfo.Keys = new Collection.Array.TreeBased.Array <KeyType>(XNewKeys, true); this.SecurityEvents = new SecurityEvents <ValueType>(); this.Events = new Events <ValueType>(); SecurityEvents.MakeKeys += (uf) => { uf.Info[KeyPos].OldKey = this.GetKey(uf.Value); }; if (IsUnique) { SecurityEvents.Updating += (uf) => { var MyInfo = uf.Info[KeyPos]; var MyValue = uf.Value; var OldKey = (KeyType)MyInfo.OldKey; var NewKey = this.GetKey(MyValue); var OldPos = KeysInfo.Keys.BinarySearch(OldKey).Index; var NewPos = 0; if (OldKey.CompareTo(NewKey) != 0) { NewPos = KeysInfo.Keys.BinarySearch(NewKey).Index; if (NewPos > -1) { throw new InvalidOperationException("Value be exist!"); } NewPos = NewPos * -1; NewPos -= 1; KeyChanging?.Invoke(new KeyChangeInfo() { NewKey = NewKey, OldKey = OldKey, Value = MyValue }); } else { NewPos = OldPos; } MyInfo.OldPos = OldPos; MyInfo.Key = NewKey; MyInfo.Pos = NewPos; }; } else { SecurityEvents.Updating += (uf) => { var MyInfo = uf.Info[KeyPos]; var MyValue = uf.Value; var OldKey = (KeyType)MyInfo.OldKey; var NewKey = this.GetKey(MyValue); var OldPos = KeysInfo.Keys.BinarySearch(OldKey).Index; var NewPos = 0; if (OldKey.CompareTo(NewKey) != 0) { NewPos = KeysInfo.Keys.BinarySearch(NewKey).Index; if (NewPos < 0) { NewPos = NewPos * -1; NewPos -= 1; } KeyChanging?.Invoke(new KeyChangeInfo() { NewKey = NewKey, OldKey = OldKey, Value = MyValue }); } else { NewPos = OldPos; } MyInfo.OldKey = OldKey; MyInfo.OldPos = OldPos; MyInfo.Key = NewKey; MyInfo.Pos = NewPos; }; } if (KeyPos == 0) { Events.Updated += (uf) => { var MyInfo = uf.Info[KeyPos]; var MyValue = uf.Value; var OldKey = (KeyType)MyInfo.OldKey; var NewKey = (KeyType)MyInfo.Key; var OldPos = MyInfo.OldPos; var NewPos = MyInfo.Pos; if (OldPos < NewPos) { NewPos -= 1; } if (OldPos != NewPos) { this.BasicActions.Items.DeleteByPosition(OldPos); this.BasicActions.Items.Insert(MyValue, NewPos); } else { this.BasicActions.Items[OldPos] = MyValue; } if (OldKey.CompareTo(NewKey) != 0) { KeysInfo.Keys.DeleteByPosition(OldPos); KeysInfo.Keys.Insert(NewKey, NewPos); KeyChanged?.Invoke(new KeyChangeInfo() { NewKey = NewKey, OldKey = OldKey, Value = MyValue }); } }; } else { Events.Updated += (uf) => { var MyInfo = uf.Info[KeyPos]; var OldKey = (KeyType)MyInfo.OldKey; var NewKey = (KeyType)MyInfo.Key; var OldPos = MyInfo.OldPos; var NewPos = MyInfo.Pos; if (OldPos < NewPos) { NewPos -= 1; } if (OldKey.CompareTo(NewKey) != 0) { KeysInfo.Keys.DeleteByPosition(OldPos); KeysInfo.Keys.Insert(NewKey, NewPos); KeyChanging?.Invoke(new KeyChangeInfo() { NewKey = NewKey, OldKey = OldKey, Value = uf.Value }); } }; } if (IsUnique) { SecurityEvents.Inserting += (info) => { var NewKey = this.GetKey(info.Value); var NewPos = KeysInfo.Keys.BinarySearch(NewKey).Index; if (NewPos > -1) { throw new InvalidOperationException("Value be exist!"); } NewPos = NewPos * -1; NewPos -= 1; var MyInfo = info.Info[KeyPos]; MyInfo.Key = NewKey; MyInfo.Pos = NewPos; }; } else { SecurityEvents.Inserting += (info) => { var NewKey = this.GetKey(info.Value); var NewPos = KeysInfo.Keys.BinarySearch(NewKey).Index; if (NewPos < 0) { NewPos = NewPos * -1; NewPos -= 1; } var MyInfo = info.Info[KeyPos]; MyInfo.Key = NewKey; MyInfo.Pos = NewPos; }; } if (KeyPos == 0) { Events.Inserted += (info) => { var MyInfo = info.Info[KeyPos]; var Pos = MyInfo.Pos; KeysInfo.Keys.Insert((KeyType)MyInfo.Key, Pos); this.BasicActions.Items.Insert(info.Value, Pos); }; } else { Events.Inserted += (info) => { var MyInfo = info.Info[KeyPos]; KeysInfo.Keys.Insert((KeyType)MyInfo.Key, MyInfo.Pos); }; } SecurityEvents.Deleting += (info) => { var MyInfo = info.Info[KeyPos]; //if (MyInfo.Key==null) //{ var OldKey = this.GetKey(info.Value); var OldPos = KeysInfo.Keys.BinarySearch(OldKey).Index; MyInfo.Key = OldKey; MyInfo.Pos = OldPos; //} }; if (KeyPos == 0) { Events.Deleted += (info) => { var Pos = info.Info[KeyPos].Pos; this.BasicActions.Items.DeleteByPosition(Pos); KeysInfo.Keys.DeleteByPosition(Pos); }; } else { Events.Deleted += (info) => { KeysInfo.Keys.DeleteByPosition(info.Info[KeyPos].Pos); }; } if (typeof(ValueType).GetInterfaces().Where((c) => c == typeof(IFactualyData)).Count() > 0) { this.Events.loading += (NewValue) => { ((IFactualyData)NewValue).Parent = this; }; this.Events.Saving += (NewValue) => { ((IFactualyData)NewValue).Parent = null; }; } if (IsUpdateAble) { ReadyForUpdateAble(); this.UpdateAble = new UpdateAbles <KeyType>(); } IgnoreUpdateAble_pos = UpdateAbles <KeyType> .IgnoreUpdateAble_Len; UpdateAbles <KeyType> .IgnoreUpdateAble_Len++; }
private void ProcessData() { // First, process buttons Debug.WriteLine(BitConverter.ToString(m_State)); // First, process the buttons byte newButtonA = (byte)((m_State[0] >> 2) & 1); // A byte newButtonB = (byte)((m_State[0] >> 1) & 1); // B byte newButtonX = (byte)((m_State[0] >> 3) & 1); // X byte newButtonY = (byte)((m_State[0] >> 0) & 1); // Y byte newButtonL1 = (byte)((m_State[0] >> 6) & 1); // L1 byte newButtonR1 = (byte)((m_State[0] >> 7) & 1); // R1 byte newButtonL2 = (byte)((m_State[1] >> 4) & 1); // L2 byte newButtonR2 = (byte)((m_State[1] >> 5) & 1); // R2 byte newButtonSelect = (byte)((m_State[0] >> 5) & 1); // Select byte newButtonStart = (byte)((m_State[0] >> 4) & 1); // Start byte newButtonDPadUp = (byte)(m_State[1] & 1); // DPad Up byte newButtonDPadDown = (byte)((m_State[1] >> 1) & 1); // DPad Down byte newButtonDPadLeft = (byte)((m_State[1] >> 2) & 1); // DPad Left byte newButtonDPadRight = (byte)((m_State[1] >> 3) & 1); // DPad Right if (buttonA != newButtonA) { KeyChanged?.Invoke(new KeyEvent(KeyCode.A, newButtonA > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonB != newButtonB) { KeyChanged?.Invoke(new KeyEvent(KeyCode.B, newButtonB > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonX != newButtonX) { KeyChanged?.Invoke(new KeyEvent(KeyCode.X, newButtonX > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonY != newButtonY) { KeyChanged?.Invoke(new KeyEvent(KeyCode.Y, newButtonY > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonL1 != newButtonL1) { KeyChanged?.Invoke(new KeyEvent(KeyCode.L1, newButtonL1 > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonR1 != newButtonR1) { KeyChanged?.Invoke(new KeyEvent(KeyCode.R1, newButtonR1 > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonL2 != newButtonL2) { KeyChanged?.Invoke(new KeyEvent(KeyCode.L2, newButtonL2 > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonR2 != newButtonR2) { KeyChanged?.Invoke(new KeyEvent(KeyCode.R2, newButtonR2 > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonSelect != newButtonSelect) { KeyChanged?.Invoke(new KeyEvent(KeyCode.Select, newButtonSelect > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonStart != newButtonStart) { KeyChanged?.Invoke(new KeyEvent(KeyCode.Start, newButtonStart > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonDPadUp != newButtonDPadUp) { KeyChanged?.Invoke(new KeyEvent(KeyCode.DPadUp, newButtonDPadUp > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonDPadDown != newButtonDPadDown) { KeyChanged?.Invoke(new KeyEvent(KeyCode.DPadDown, newButtonDPadDown > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonDPadLeft != newButtonDPadLeft) { KeyChanged?.Invoke(new KeyEvent(KeyCode.DPadLeft, newButtonDPadLeft > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } if (buttonDPadRight != newButtonDPadRight) { KeyChanged?.Invoke(new KeyEvent(KeyCode.DPadRight, newButtonDPadRight > 0 ? ControllerAction.Pressed : ControllerAction.Unpressed)); } buttonA = newButtonA; buttonB = newButtonB; buttonX = newButtonX; buttonY = newButtonY; buttonL1 = newButtonL1; buttonR1 = newButtonR1; buttonL2 = newButtonL2; buttonR2 = newButtonR2; buttonSelect = newButtonSelect; buttonStart = newButtonStart; buttonDPadUp = newButtonDPadUp; buttonDPadDown = newButtonDPadDown; buttonDPadLeft = newButtonDPadLeft; buttonDPadRight = newButtonDPadRight; // Is it "Moga Mobile" controller? if (ControllerName.Equals("BD&A")) { // Next, process joystick/d-pad axes int newAxisX = 0; sbyte newDataX = (sbyte)m_State[2]; if ((byte)(m_State[1] & 0x04) == 0x04 || (byte)(m_State[1] & 0x08) == 0x08) { newAxisX = (byte)(m_State[1] & 0x04) == 0x04 ? 1 : -1; } if (axisX != newAxisX || (newAxisX != 0 && dataX != newDataX) || (axisX != 0 && newAxisX == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.X, newAxisX == 0 ? 0 : newDataX)); } axisX = newAxisX; dataX = newDataX; int newAxisY = 0; sbyte newDataY = (sbyte)m_State[3]; if ((byte)(m_State[1] & 0x01) == 0x01 || (byte)(m_State[1] & 0x02) == 2) { newAxisY = (byte)(m_State[1] & 0x01) == 0x01 ? 1 : -1; } if (axisY != newAxisY || (newAxisY != 0 && dataY != newDataY) || (axisY != 0 && newAxisY == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.Y, newAxisY == 0 ? 0 : newDataY)); } axisY = newAxisY; dataY = newDataY; int newAxisZ = 0; sbyte newDataZ = (sbyte)m_State[5]; if ((byte)(m_State[1] & 0x10) == 0x10 || (byte)(m_State[1] & 0x20) == 0x20) { newAxisZ = (byte)(m_State[1] & 0x10) == 0x10 ? 1 : -1; } if (axisZ != newAxisZ || (newAxisZ != 0 && dataZ != newDataZ) || (axisZ != 0 && newAxisZ == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.Z, newAxisZ == 0 ? 0 : newDataZ)); } axisZ = newAxisZ; dataZ = newDataZ; int newAxisRZ = 0; sbyte newDataRZ = (sbyte)m_State[4]; if ((byte)(m_State[1] & 0x40) == 0x40 || (byte)(m_State[1] & 0x80) == 0x80) { newAxisRZ = (byte)(m_State[1] & 0x40) == 0x40 ? 1 : -1; } if (axisRZ != newAxisRZ || (newAxisRZ != 0 && dataRZ != newDataRZ) || (axisRZ != 0 && newAxisRZ == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.RZ, newAxisRZ == 0 ? 0 : newDataRZ)); } axisRZ = newAxisRZ; dataRZ = newDataRZ; } else { // Next, process joystick/d-pad axes int newAxisX = 0; sbyte newDataX = (sbyte)m_State[2]; if ((byte)(m_State[2] & 0x80) == 0x80 || (byte)(m_State[2] & 0x40) == 0x40) { newAxisX = (byte)(m_State[2] & 0x80) == 0x80 ? 1 : -1; } if (axisX != newAxisX || (newAxisX != 0 && dataX != newDataX) || (axisX != 0 && newAxisX == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.X, newDataX)); } axisX = newAxisX; dataX = newDataX; int newAxisY = 0; sbyte newDataY = (sbyte)m_State[3]; if ((byte)(m_State[3] & 0x80) == 0x80 || (byte)(m_State[3] & 0x40) == 0x40) { newAxisY = (byte)(m_State[3] & 0x80) == 0x80 ? 1 : -1; } if (axisY != newAxisY || (newAxisY != 0 && dataY != newDataY) || (axisY != 0 && newAxisY == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.Y, newDataY)); } axisY = newAxisY; dataY = newDataY; int newAxisZ = 0; sbyte newDataZ = (sbyte)m_State[5]; if ((byte)(m_State[5] & 0x80) == 0x80 || (byte)(m_State[5] & 0x40) == 0x40) { newAxisZ = (byte)(m_State[5] & 0x80) == 0x80 ? 1 : -1; } if (axisZ != newAxisZ || (newAxisZ != 0 && dataZ != newDataZ) || (axisZ != 0 && newAxisZ == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.Z, newAxisZ == 0 ? 0 : newDataZ)); } axisZ = newAxisZ; dataZ = newDataZ; int newAxisRZ = 0; sbyte newDataRZ = (sbyte)m_State[4]; if ((byte)(m_State[4] & 0x80) == 0x80 || (byte)(m_State[4] & 0x40) == 0x40) { newAxisRZ = (byte)(m_State[4] & 0x80) == 0x80 ? 1 : -1; } if (axisRZ != newAxisRZ || (newAxisRZ != 0 && dataRZ != newDataRZ) || (axisRZ != 0 && newAxisRZ == 0)) { AxisChanged?.Invoke(new MotionEvent(Axis.RZ, newAxisRZ == 0 ? 0 : newDataRZ)); } axisRZ = newAxisRZ; dataRZ = newDataRZ; } }
private void OnKeyChanged(EventArgs e) { RegisterHotKey(); KeyChanged?.Invoke(this, e); }
public void OnKeyChanged() { KeyChanged?.Invoke(this, new EventArgs()); }