diff options
| author | Roy <roy.mail.net@gmail.com> | 2018-05-22 22:50:06 +0300 |
|---|---|---|
| committer | Roy <roy.mail.net@gmail.com> | 2018-05-22 22:50:06 +0300 |
| commit | 6faef62d683a670aa85ca4a4ff0c39cf1b3002ed (patch) | |
| tree | a5d9fd09dcfdd916d99e2e3687420ea9610452e9 /Software/Visual_Studio/Tango.Touch/Keyboard/TouchKeyboard.cs | |
| parent | d719d039f355e207245b083f7db76703700c6c79 (diff) | |
| download | Tango-6faef62d683a670aa85ca4a4ff0c39cf1b3002ed.tar.gz Tango-6faef62d683a670aa85ca4a4ff0c39cf1b3002ed.zip | |
Started working on Tango.Touch project
Diffstat (limited to 'Software/Visual_Studio/Tango.Touch/Keyboard/TouchKeyboard.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Touch/Keyboard/TouchKeyboard.cs | 465 |
1 files changed, 465 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Touch/Keyboard/TouchKeyboard.cs b/Software/Visual_Studio/Tango.Touch/Keyboard/TouchKeyboard.cs new file mode 100644 index 000000000..d92fe4bb8 --- /dev/null +++ b/Software/Visual_Studio/Tango.Touch/Keyboard/TouchKeyboard.cs @@ -0,0 +1,465 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using Forms = System.Windows.Forms; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using Tango.Core.Commands; +using System.Threading; +using System.Collections.ObjectModel; + +namespace Tango.Touch.Keyboard +{ + /// <summary> + /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file. + /// + /// Step 1a) Using this custom control in a XAML file that exists in the current project. + /// Add this XmlNamespace attribute to the root element of the markup file where it is + /// to be used: + /// + /// xmlns:MyNamespace="clr-namespace:Tango.Touch.Keyboard" + /// + /// + /// Step 1b) Using this custom control in a XAML file that exists in a different project. + /// Add this XmlNamespace attribute to the root element of the markup file where it is + /// to be used: + /// + /// xmlns:MyNamespace="clr-namespace:Tango.Touch.Keyboard;assembly=Tango.Touch.Keyboard" + /// + /// You will also need to add a project reference from the project where the XAML file lives + /// to this project and Rebuild to avoid compilation errors: + /// + /// Right click on the target project in the Solution Explorer and + /// "Add Reference"->"Projects"->[Browse to and select this project] + /// + /// + /// Step 2) + /// Go ahead and use your control in the XAML file. + /// + /// <MyNamespace:TouchKeyboard/> + /// + /// </summary> + public class TouchKeyboard : Control + { + static TouchKeyboard() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchKeyboard), new FrameworkPropertyMetadata(typeof(TouchKeyboard))); + } + + private bool _isInitialized; + + public event EventHandler<KeyboardActionKeyMode> ActionKeyPressed; + + public bool IsSpecialCharactersOn + { + get { return (bool)GetValue(IsSpecialCharactersOnProperty); } + set { SetValue(IsSpecialCharactersOnProperty, value); } + } + public static readonly DependencyProperty IsSpecialCharactersOnProperty = + DependencyProperty.Register("IsSpecialCharactersOn", typeof(bool), typeof(TouchKeyboard), new PropertyMetadata(false, (d, e) => (d as TouchKeyboard).OnSpecialCharactersOnChanged())); + + public TouchKeyboardMode Mode + { + get { return (TouchKeyboardMode)GetValue(ModeProperty); } + set { SetValue(ModeProperty, value); } + } + public static readonly DependencyProperty ModeProperty = + DependencyProperty.Register("Mode", typeof(TouchKeyboardMode), typeof(TouchKeyboard), new PropertyMetadata(TouchKeyboardMode.AlphaNumeric)); + + public KeyboardActionKeyMode ActionKeyMode + { + get { return (KeyboardActionKeyMode)GetValue(ActionKeyModeProperty); } + set { SetValue(ActionKeyModeProperty, value); } + } + public static readonly DependencyProperty ActionKeyModeProperty = + DependencyProperty.Register("ActionKeyMode", typeof(KeyboardActionKeyMode), typeof(TouchKeyboard), new PropertyMetadata(KeyboardActionKeyMode.Tab, (d, e) => (d as TouchKeyboard).OnActionKeyModeChanged())); + + public String ActionKeyText + { + get { return (String)GetValue(ActionKeyTextProperty); } + private set { SetValue(ActionKeyTextProperty, value); } + } + public static readonly DependencyProperty ActionKeyTextProperty = + DependencyProperty.Register("ActionKeyText", typeof(String), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public KeyboardDefinition CurrentKeyboardDefinition + { + get { return (KeyboardDefinition)GetValue(CurrentKeyboardDefinitionProperty); } + set { SetValue(CurrentKeyboardDefinitionProperty, value); } + } + public static readonly DependencyProperty CurrentKeyboardDefinitionProperty = + DependencyProperty.Register("CurrentKeyboardDefinition", typeof(KeyboardDefinition), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public int CurrentKeyboardDefinitionIndex + { + get { return (int)GetValue(CurrentKeyboardDefinitionIndexProperty); } + set { SetValue(CurrentKeyboardDefinitionIndexProperty, value); } + } + public static readonly DependencyProperty CurrentKeyboardDefinitionIndexProperty = + DependencyProperty.Register("CurrentKeyboardDefinitionIndex", typeof(int), typeof(TouchKeyboard), new PropertyMetadata(0)); + + public ObservableCollection<KeyboardDefinition> KeyboardDefinitions + { + get { return (ObservableCollection<KeyboardDefinition>)GetValue(KeyboardDefinitionsProperty); } + set { SetValue(KeyboardDefinitionsProperty, value); } + } + public static readonly DependencyProperty KeyboardDefinitionsProperty = + DependencyProperty.Register("KeyboardDefinitions", typeof(ObservableCollection<KeyboardDefinition>), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public Size KeySize + { + get { return (Size)GetValue(KeySizeProperty); } + set { SetValue(KeySizeProperty, value); } + } + public static readonly DependencyProperty KeySizeProperty = + DependencyProperty.Register("KeySize", typeof(Size), typeof(TouchKeyboard), new PropertyMetadata(Size.Empty)); + + public double MaxSpecialKeyWidth + { + get { return (double)GetValue(MaxSpecialKeyWidthProperty); } + set { SetValue(MaxSpecialKeyWidthProperty, value); } + } + public static readonly DependencyProperty MaxSpecialKeyWidthProperty = + DependencyProperty.Register("MaxSpecialKeyWidth", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(0.0)); + + public KeysLineDefinition NumbersLineDefinition + { + get { return (KeysLineDefinition)GetValue(NumbersLineDefinitionProperty); } + set { SetValue(NumbersLineDefinitionProperty, value); } + } + public static readonly DependencyProperty NumbersLineDefinitionProperty = + DependencyProperty.Register("NumbersLineDefinition", typeof(KeysLineDefinition), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public Brush CharactersBackground + { + get { return (Brush)GetValue(CharactersBackgroundProperty); } + set { SetValue(CharactersBackgroundProperty, value); } + } + public static readonly DependencyProperty CharactersBackgroundProperty = + DependencyProperty.Register("CharactersBackground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(Brushes.Gainsboro)); + + public Brush CharactersForeground + { + get { return (Brush)GetValue(CharactersForegroundProperty); } + set { SetValue(CharactersForegroundProperty, value); } + } + public static readonly DependencyProperty CharactersForegroundProperty = + DependencyProperty.Register("CharactersForeground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(20, 20, 20)))); + + public Brush SpecialCharactersBackground + { + get { return (Brush)GetValue(SpecialCharactersBackgroundProperty); } + set { SetValue(SpecialCharactersBackgroundProperty, value); } + } + public static readonly DependencyProperty SpecialCharactersBackgroundProperty = + DependencyProperty.Register("SpecialCharactersBackground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(Brushes.Gray)); + + public Brush SpecialCharactersForeground + { + get { return (Brush)GetValue(SpecialCharactersForegroundProperty); } + set { SetValue(SpecialCharactersForegroundProperty, value); } + } + public static readonly DependencyProperty SpecialCharactersForegroundProperty = + DependencyProperty.Register("SpecialCharactersForeground", typeof(Brush), typeof(TouchKeyboard), new PropertyMetadata(Brushes.Gainsboro)); + + public double VectorMarkupSize + { + get { return (double)GetValue(VectorMarkupSizeProperty); } + set { SetValue(VectorMarkupSizeProperty, value); } + } + public static readonly DependencyProperty VectorMarkupSizeProperty = + DependencyProperty.Register("VectorMarkupSize", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(10.0)); + + public double DotsSize + { + get { return (double)GetValue(DotsSizeProperty); } + set { SetValue(DotsSizeProperty, value); } + } + public static readonly DependencyProperty DotsSizeProperty = + DependencyProperty.Register("DotsSize", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(5.0)); + + public double CharactersCornerRadius + { + get { return (double)GetValue(CharactersCornerRadiusProperty); } + set { SetValue(CharactersCornerRadiusProperty, value); } + } + public static readonly DependencyProperty CharactersCornerRadiusProperty = + DependencyProperty.Register("CharactersCornerRadius", typeof(double), typeof(TouchKeyboard), new PropertyMetadata(5.0)); + + public CapsLockMode CapsLockMode + { + get { return (CapsLockMode)GetValue(CapsLockModeProperty); } + set { SetValue(CapsLockModeProperty, value); } + } + public static readonly DependencyProperty CapsLockModeProperty = + DependencyProperty.Register("CapsLockMode", typeof(CapsLockMode), typeof(TouchKeyboard), new PropertyMetadata(CapsLockMode.None)); + + public bool IsCapsLockOn + { + get { return (bool)GetValue(IsCapsLockOnProperty); } + private set { SetValue(IsCapsLockOnProperty, value); } + } + public static readonly DependencyProperty IsCapsLockOnProperty = + DependencyProperty.Register("IsCapsLockOn", typeof(bool), typeof(TouchKeyboard), new PropertyMetadata(false)); + + public RelayCommand<KeyDefinition> KeyDefinitionCommand + { + get { return (RelayCommand<KeyDefinition>)GetValue(KeyDefinitionCommandProperty); } + set { SetValue(KeyDefinitionCommandProperty, value); } + } + public static readonly DependencyProperty KeyDefinitionCommandProperty = + DependencyProperty.Register("KeyDefinitionCommand", typeof(RelayCommand<KeyDefinition>), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public RelayCommand<SpecialKeyDefinition> SpecialKeyDefinitionCommand + { + get { return (RelayCommand<SpecialKeyDefinition>)GetValue(SpecialKeyDefinitionCommandProperty); } + set { SetValue(SpecialKeyDefinitionCommandProperty, value); } + } + public static readonly DependencyProperty SpecialKeyDefinitionCommandProperty = + DependencyProperty.Register("SpecialKeyDefinitionCommand", typeof(RelayCommand<SpecialKeyDefinition>), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public RelayCommand SpecialCharactersCommand + { + get { return (RelayCommand)GetValue(SpecialCharactersCommandProperty); } + set { SetValue(SpecialCharactersCommandProperty, value); } + } + public static readonly DependencyProperty SpecialCharactersCommandProperty = + DependencyProperty.Register("SpecialCharactersCommand", typeof(RelayCommand), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public RelayCommand<String> FreeTextCommand + { + get { return (RelayCommand<String>)GetValue(FreeTextCommandProperty); } + set { SetValue(FreeTextCommandProperty, value); } + } + public static readonly DependencyProperty FreeTextCommandProperty = + DependencyProperty.Register("FreeTextCommand", typeof(RelayCommand<String>), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public RelayCommand NextLanguageCommand + { + get { return (RelayCommand)GetValue(NextLanguageCommandProperty); } + set { SetValue(NextLanguageCommandProperty, value); } + } + public static readonly DependencyProperty NextLanguageCommandProperty = + DependencyProperty.Register("NextLanguageCommand", typeof(RelayCommand), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public RelayCommand ActionKeyCommand + { + get { return (RelayCommand)GetValue(ActionKeyCommandProperty); } + set { SetValue(ActionKeyCommandProperty, value); } + } + public static readonly DependencyProperty ActionKeyCommandProperty = + DependencyProperty.Register("ActionKeyCommand", typeof(RelayCommand), typeof(TouchKeyboard), new PropertyMetadata(null)); + + public RelayCommand<Object> NumericKeyCommand + { + get { return (RelayCommand<Object>)GetValue(NumericKeyCommandProperty); } + set { SetValue(NumericKeyCommandProperty, value); } + } + public static readonly DependencyProperty NumericKeyCommandProperty = + DependencyProperty.Register("NumericKeyCommand", typeof(RelayCommand<Object>), typeof(TouchKeyboard), new PropertyMetadata(null)); + + + public TouchKeyboard() + { + KeyboardDefinitions = new ObservableCollection<KeyboardDefinition>(); + Initialize(); + + this.SizeChanged += (_, __) => SetKeySize(); + KeyDefinitionCommand = new RelayCommand<KeyDefinition>(InvokeKeyDefinition); + SpecialKeyDefinitionCommand = new RelayCommand<SpecialKeyDefinition>(InvokeSpecialKeyDefinition); + SpecialCharactersCommand = new RelayCommand(ToggleSpecialKeys); + FreeTextCommand = new RelayCommand<string>((x) => SendKeys(x)); + NextLanguageCommand = new RelayCommand(NextLanguage); + ActionKeyCommand = new RelayCommand(InvokeActionKey); + NumericKeyCommand = new RelayCommand<object>((x) => SendKeys(x.ToString().Replace("'", ""))); + } + + private void Initialize() + { + if (!_isInitialized) + { + var numbers = new KeysLineDefinition(); + + for (int i = 1; i < 10; i++) + { + numbers.KeyDefinitions.Add(new KeyDefinition() + { + StandardText = i.ToString(), + }); + } + + numbers.KeyDefinitions.Add(new KeyDefinition() + { + StandardText = "0", + }); + + NumbersLineDefinition = numbers; + + CurrentKeyboardDefinition = KeyboardDefinition.Default; + KeyboardDefinitions.Add(CurrentKeyboardDefinition); + KeyboardDefinitions.Add(KeyboardDefinition.CreateDefaultHebrewAlphaNumeric()); + + SetKeySize(); + + OnActionKeyModeChanged(); + + _isInitialized = true; + } + } + + private void SetKeySize() + { + KeySize = new Size(ActualWidth / NumbersLineDefinition.KeyDefinitions.Count, (ActualWidth / NumbersLineDefinition.KeyDefinitions.Count) * 0.90); + MaxSpecialKeyWidth = KeySize.Width * 2; + } + + private void InvokeKeyDefinition(KeyDefinition key) + { + if (IsCapsLockOn) + { + SendKeys(key.CapsText); + } + else + { + //UIAutomationHelper.GetActiveWindow(); + + //IInputElement focusedControl = FocusManager.GetFocusedElement(this); + + SendKeys(key.StandardText); + } + } + + private void InvokeSpecialKeyDefinition(SpecialKeyDefinition key) + { + switch (key.Type) + { + case SpecialKeyType.StandardText: + SendKeys(key.Output); + break; + case SpecialKeyType.CapsLock: + + if (CapsLockMode == CapsLockMode.None) + { + CapsLockMode = CapsLockMode.SingleChar; + } + else if (CapsLockMode == CapsLockMode.SingleChar) + { + CapsLockMode = CapsLockMode.Locked; + } + else + { + CapsLockMode = CapsLockMode.None; + } + + IsCapsLockOn = CapsLockMode != CapsLockMode.None; + + break; + } + } + + private Task SendKeys(String key) + { + Application.Current.MainWindow.Activate(); + + var isSpecialChar = IsSpecialCharactersOn; + var definition = CurrentKeyboardDefinition; + + if (CapsLockMode == CapsLockMode.SingleChar) + { + CapsLockMode = CapsLockMode.None; + IsCapsLockOn = false; + } + + return Task.Factory.StartNew(() => + { + if (isSpecialChar && definition.KeysLinesDefinitions.SelectMany(x => x.KeyDefinitions).Select(x => x.StandardText).Contains(key)) + { + Forms.SendKeys.SendWait("{" + key + "}"); + } + else + { + Forms.SendKeys.SendWait(key); + } + }); + } + + private void OnSpecialCharactersOnChanged() + { + if (!IsSpecialCharactersOn) + { + CurrentKeyboardDefinition = KeyboardDefinitions[CurrentKeyboardDefinitionIndex]; + } + else + { + KeyboardDefinition definition = new KeyboardDefinition(); + definition.EnterText = CurrentKeyboardDefinition.EnterText; + definition.KeysLinesDefinitions = KeyboardDefinition.CreateDefaultSpecialCharacters(); + CurrentKeyboardDefinition = definition; + } + } + + private void ToggleSpecialKeys() + { + IsSpecialCharactersOn = !IsSpecialCharactersOn; + } + + private void NextLanguage() + { + IsCapsLockOn = false; + + CurrentKeyboardDefinitionIndex++; + + if (CurrentKeyboardDefinitionIndex > KeyboardDefinitions.Count - 1) + { + CurrentKeyboardDefinitionIndex = 0; + } + + CurrentKeyboardDefinition = KeyboardDefinitions[CurrentKeyboardDefinitionIndex]; + + OnActionKeyModeChanged(); + } + + private void OnActionKeyModeChanged() + { + switch (ActionKeyMode) + { + case KeyboardActionKeyMode.Enter: + ActionKeyText = CurrentKeyboardDefinition.EnterText; + break; + case KeyboardActionKeyMode.Tab: + ActionKeyText = CurrentKeyboardDefinition.TabText; + break; + case KeyboardActionKeyMode.Search: + ActionKeyText = "Search"; // TODO: Make trigger in view and vector path. + break; + } + } + + private void InvokeActionKey() + { + switch (ActionKeyMode) + { + case KeyboardActionKeyMode.Enter: + SendKeys("{ENTER}"); + break; + case KeyboardActionKeyMode.Tab: + SendKeys("{TAB}"); + break; + case KeyboardActionKeyMode.Search: + SendKeys("{ENTER}"); + break; + } + + ActionKeyPressed?.Invoke(this, ActionKeyMode); + } + } +} |
