private void UserControl_KeyDown(object sender, KeyEventArgs e) { if (KeyValidator.IsUpKey(e.Key)) { e.Handled = true; MoveUp?.Invoke(this, new EventArgs()); return; } else if (KeyValidator.IsDownKey(e.Key)) { e.Handled = true; MoveDown?.Invoke(this, new EventArgs()); return; } else if (KeyValidator.IsLeftKey(e.Key)) { e.Handled = true; MoveLeft?.Invoke(this, new EventArgs()); return; } else if (KeyValidator.IsRightKey(e.Key)) { e.Handled = true; MoveRight?.Invoke(this, new EventArgs()); return; } else if (KeyValidator.IsPageDownKey(e.Key)) { e.Handled = true; MovePageDown?.Invoke(this, new EventArgs()); return; } else if (KeyValidator.IsPageUpKey(e.Key)) { e.Handled = true; MovePageUp?.Invoke(this, new EventArgs()); return; } else if (KeyValidator.IsDeleteKey(e.Key)) { if (!ReadOnlyMode) { e.Handled = true; ByteDeleted?.Invoke(this, new EventArgs()); return; } } else if (KeyValidator.IsBackspaceKey(e.Key)) { e.Handled = true; ByteDeleted?.Invoke(this, new EventArgs()); MovePrevious?.Invoke(this, new EventArgs()); return; } else if (KeyValidator.IsEscapeKey(e.Key)) { e.Handled = true; EscapeKey?.Invoke(this, new EventArgs()); return; } //MODIFY BYTE if (!ReadOnlyMode) { if (KeyValidator.IsHexKey(e.Key)) { string key; if (KeyValidator.IsNumericKey(e.Key)) { key = KeyValidator.GetDigitFromKey(e.Key).ToString(); } else { key = e.Key.ToString().ToLower(); } switch (_keyDownLabel) { case KeyDownLabel.FirstChar: FirstHexChar.Content = key; _keyDownLabel = KeyDownLabel.SecondChar; Action = ByteAction.Modified; Byte = ByteConverters.HexToByte(FirstHexChar.Content.ToString() + SecondHexChar.Content.ToString())[0]; break; case KeyDownLabel.SecondChar: SecondHexChar.Content = key; _keyDownLabel = KeyDownLabel.NextPosition; Action = ByteAction.Modified; Byte = ByteConverters.HexToByte(FirstHexChar.Content.ToString() + SecondHexChar.Content.ToString())[0]; //Move focus event MoveNext?.Invoke(this, new EventArgs()); break; } } } }
protected override void OnKeyDown(KeyEventArgs e) { if (Byte == null) { return; } if (KeyValidation(e)) { return; } //MODIFY BYTE if (!ReadOnlyMode && KeyValidator.IsHexKey(e.Key)) { switch (_parent.DataStringVisual) { case DataVisualType.Hexadecimal: #region Edit hexadecimal value string key; key = KeyValidator.IsNumericKey(e.Key) ? KeyValidator.GetDigitFromKey(e.Key).ToString() : e.Key.ToString().ToLower(); //Update byte var byteValueCharArray = ByteConverters.ByteToHexCharArray(Byte.Value); switch (_keyDownLabel) { case KeyDownLabel.FirstChar: byteValueCharArray[0] = key.ToCharArray()[0]; _keyDownLabel = KeyDownLabel.SecondChar; Action = ByteAction.Modified; Byte = ByteConverters.HexToByte( byteValueCharArray[0] + byteValueCharArray[1].ToString())[0]; break; case KeyDownLabel.SecondChar: byteValueCharArray[1] = key.ToCharArray()[0]; _keyDownLabel = KeyDownLabel.NextPosition; Action = ByteAction.Modified; Byte = ByteConverters.HexToByte( byteValueCharArray[0] + byteValueCharArray[1].ToString())[0]; //Insert byte at end of file if (_parent.Length != BytePositionInFile + 1) { _keyDownLabel = KeyDownLabel.NextPosition; OnMoveNext(new EventArgs()); } break; case KeyDownLabel.NextPosition: //byte[] byteToAppend = { (byte)key.ToCharArray()[0] }; _parent.AppendByte(new byte[] { 0 }); OnMoveNext(new EventArgs()); break; } #endregion break; case DataVisualType.Decimal: //Not editable at this moment, maybe in future break; } } UpdateCaret(); base.OnKeyDown(e); }