Server/Events/EventHid.cs
author StephaneLenclud
Sat, 20 Aug 2016 21:00:35 +0200
changeset 246 30a221eecc06
parent 244 2e4d2558bb21
child 247 afdbe76ab03b
permissions -rw-r--r--
Generic HID event with key recognition functional.
StephaneLenclud@244
     1
using System;
StephaneLenclud@244
     2
using System.Collections.Generic;
StephaneLenclud@244
     3
using System.Linq;
StephaneLenclud@244
     4
using System.Reflection;
StephaneLenclud@244
     5
using System.Text;
StephaneLenclud@244
     6
using System.Threading.Tasks;
StephaneLenclud@244
     7
using System.Runtime.Serialization;
StephaneLenclud@244
     8
using System.Windows.Forms;
StephaneLenclud@244
     9
using Ear = SharpLib.Ear;
StephaneLenclud@244
    10
using Hid = SharpLib.Hid;
StephaneLenclud@244
    11
StephaneLenclud@244
    12
namespace SharpDisplayManager
StephaneLenclud@244
    13
{
StephaneLenclud@244
    14
    [DataContract]
StephaneLenclud@244
    15
    [Ear.AttributeObject(Id = "Event.Hid", Name = "HID", Description = "Corresponding HID message received.")]
StephaneLenclud@244
    16
    public class EventHid: Ear.Event
StephaneLenclud@244
    17
    {
StephaneLenclud@244
    18
        public EventHid()
StephaneLenclud@244
    19
        {
StephaneLenclud@244
    20
        }
StephaneLenclud@244
    21
StephaneLenclud@244
    22
        [DataMember]
StephaneLenclud@244
    23
        public ushort UsagePage { get; set; }
StephaneLenclud@244
    24
StephaneLenclud@244
    25
        [DataMember]
StephaneLenclud@244
    26
        public ushort UsageCollection { get; set; }
StephaneLenclud@244
    27
StephaneLenclud@244
    28
        [DataMember]
StephaneLenclud@244
    29
        public ushort Usage { get; set; }
StephaneLenclud@244
    30
StephaneLenclud@244
    31
        [DataMember]
StephaneLenclud@244
    32
        public Keys Key { get; set; }
StephaneLenclud@244
    33
StephaneLenclud@244
    34
        [DataMember]
StephaneLenclud@244
    35
        [Ear.AttributeObjectProperty
StephaneLenclud@244
    36
            (
StephaneLenclud@244
    37
            Id = "HID.Keyboard.IsKeyUp",
StephaneLenclud@244
    38
            Name = "Key Up",
StephaneLenclud@244
    39
            Description = "Key up if set, key down otherwise."
StephaneLenclud@244
    40
            )]
StephaneLenclud@244
    41
        public bool IsKeyUp { get; set; } = false;
StephaneLenclud@244
    42
StephaneLenclud@244
    43
        [DataMember]
StephaneLenclud@246
    44
        public bool IsMouse { get; set; } = false;
StephaneLenclud@244
    45
StephaneLenclud@244
    46
        [DataMember]
StephaneLenclud@246
    47
        public bool IsKeyboard { get; set; } = false;
StephaneLenclud@244
    48
StephaneLenclud@244
    49
        [DataMember]
StephaneLenclud@246
    50
        public bool IsGeneric { get; set; } = false;
StephaneLenclud@244
    51
StephaneLenclud@244
    52
        [DataMember]
StephaneLenclud@244
    53
        public bool HasModifierShift { get; set; } = false;
StephaneLenclud@244
    54
StephaneLenclud@244
    55
        [DataMember]
StephaneLenclud@244
    56
        public bool HasModifierControl { get; set; } = false;
StephaneLenclud@244
    57
StephaneLenclud@244
    58
        [DataMember]
StephaneLenclud@244
    59
        public bool HasModifierAlt { get; set; } = false;
StephaneLenclud@244
    60
StephaneLenclud@244
    61
        [DataMember]
StephaneLenclud@244
    62
        public bool HasModifierWindows { get; set; } = false;
StephaneLenclud@244
    63
StephaneLenclud@246
    64
        [DataMember]
StephaneLenclud@246
    65
        public string PersistedBrief { get; set; } = "Press a key";
StephaneLenclud@246
    66
StephaneLenclud@246
    67
StephaneLenclud@244
    68
StephaneLenclud@244
    69
        protected override void DoConstruct()
StephaneLenclud@244
    70
        {
StephaneLenclud@244
    71
            base.DoConstruct();
StephaneLenclud@244
    72
            UpdateDynamicProperties();
StephaneLenclud@244
    73
        }
StephaneLenclud@244
    74
StephaneLenclud@244
    75
        private void UpdateDynamicProperties()
StephaneLenclud@244
    76
        {
StephaneLenclud@244
    77
            
StephaneLenclud@244
    78
        }
StephaneLenclud@244
    79
StephaneLenclud@244
    80
        /// <summary>
StephaneLenclud@244
    81
        /// Make sure we distinguish between various configuration of this event 
StephaneLenclud@244
    82
        /// </summary>
StephaneLenclud@244
    83
        /// <returns></returns>
StephaneLenclud@244
    84
        public override string Brief()
StephaneLenclud@244
    85
        {
StephaneLenclud@244
    86
            string brief = Name + ": ";
StephaneLenclud@244
    87
StephaneLenclud@244
    88
            if (IsKeyboard)
StephaneLenclud@244
    89
            {
StephaneLenclud@244
    90
                brief += Key.ToString();
StephaneLenclud@244
    91
StephaneLenclud@244
    92
                if (HasModifierAlt)
StephaneLenclud@244
    93
                {
StephaneLenclud@244
    94
                    brief += " + ALT";
StephaneLenclud@244
    95
                }
StephaneLenclud@244
    96
StephaneLenclud@244
    97
                if (HasModifierControl)
StephaneLenclud@244
    98
                {
StephaneLenclud@244
    99
                    brief += " + CTRL";
StephaneLenclud@244
   100
                }
StephaneLenclud@244
   101
StephaneLenclud@244
   102
                if (HasModifierShift)
StephaneLenclud@244
   103
                {
StephaneLenclud@244
   104
                    brief += " + SHIFT";
StephaneLenclud@244
   105
                }
StephaneLenclud@244
   106
StephaneLenclud@244
   107
                if (HasModifierWindows)
StephaneLenclud@244
   108
                {
StephaneLenclud@244
   109
                    brief += " + WIN";
StephaneLenclud@244
   110
                }
StephaneLenclud@244
   111
            }
StephaneLenclud@244
   112
            else if (IsGeneric)
StephaneLenclud@244
   113
            {
StephaneLenclud@246
   114
                brief += PersistedBrief;
StephaneLenclud@244
   115
            }
StephaneLenclud@244
   116
StephaneLenclud@244
   117
            if (IsKeyUp)
StephaneLenclud@244
   118
            {
StephaneLenclud@244
   119
                brief += " (UP)";
StephaneLenclud@244
   120
            }
StephaneLenclud@244
   121
            else
StephaneLenclud@244
   122
            {
StephaneLenclud@244
   123
                brief += " (DOWN)";
StephaneLenclud@244
   124
            }
StephaneLenclud@244
   125
StephaneLenclud@244
   126
            return brief;
StephaneLenclud@244
   127
        }
StephaneLenclud@244
   128
StephaneLenclud@244
   129
        /// <summary>
StephaneLenclud@244
   130
        ///
StephaneLenclud@244
   131
        /// </summary>
StephaneLenclud@244
   132
        /// <param name="obj"></param>
StephaneLenclud@244
   133
        /// <returns></returns>
StephaneLenclud@244
   134
        public override bool Equals(object obj)
StephaneLenclud@244
   135
        {
StephaneLenclud@244
   136
            if (obj is EventHidKeyboard)
StephaneLenclud@244
   137
            {
StephaneLenclud@244
   138
                EventHidKeyboard e = (EventHidKeyboard)obj;
StephaneLenclud@244
   139
                return e.Key == Key
StephaneLenclud@244
   140
                        && e.IsKeyUp == IsKeyUp
StephaneLenclud@244
   141
                        && e.HasModifierAlt == HasModifierAlt
StephaneLenclud@244
   142
                        && e.HasModifierControl == HasModifierControl
StephaneLenclud@244
   143
                        && e.HasModifierShift == HasModifierShift
StephaneLenclud@244
   144
                        && e.HasModifierWindows == HasModifierWindows;
StephaneLenclud@244
   145
            }
StephaneLenclud@244
   146
StephaneLenclud@244
   147
            return false;
StephaneLenclud@244
   148
        }
StephaneLenclud@246
   149
StephaneLenclud@246
   150
StephaneLenclud@246
   151
        /// <summary>
StephaneLenclud@246
   152
        /// 
StephaneLenclud@246
   153
        /// </summary>
StephaneLenclud@246
   154
        protected override void OnStateLeave()
StephaneLenclud@246
   155
        {
StephaneLenclud@246
   156
            if (CurrentState == State.Edit)
StephaneLenclud@246
   157
            {
StephaneLenclud@246
   158
                // Leaving edit mode
StephaneLenclud@246
   159
                // Unhook HID events
StephaneLenclud@246
   160
                Program.HidHandler.OnHidEvent -= HandleHidEvent;
StephaneLenclud@246
   161
StephaneLenclud@246
   162
            }
StephaneLenclud@246
   163
        }
StephaneLenclud@246
   164
StephaneLenclud@246
   165
        /// <summary>
StephaneLenclud@246
   166
        /// 
StephaneLenclud@246
   167
        /// </summary>
StephaneLenclud@246
   168
        protected override void OnStateEnter()
StephaneLenclud@246
   169
        {
StephaneLenclud@246
   170
            if (CurrentState == State.Edit)
StephaneLenclud@246
   171
            {
StephaneLenclud@246
   172
                // Enter edit mode
StephaneLenclud@246
   173
                // Hook-in HID events
StephaneLenclud@246
   174
                Program.HidHandler.OnHidEvent += HandleHidEvent;
StephaneLenclud@246
   175
StephaneLenclud@246
   176
            }
StephaneLenclud@246
   177
        }
StephaneLenclud@246
   178
StephaneLenclud@246
   179
        /// <summary>
StephaneLenclud@246
   180
        /// Here we receive HID events from our HID library.
StephaneLenclud@246
   181
        /// </summary>
StephaneLenclud@246
   182
        /// <param name="aSender"></param>
StephaneLenclud@246
   183
        /// <param name="aHidEvent"></param>
StephaneLenclud@246
   184
        public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent)
StephaneLenclud@246
   185
        {
StephaneLenclud@246
   186
            if (CurrentState != State.Edit
StephaneLenclud@246
   187
                || aHidEvent.IsMouse
StephaneLenclud@246
   188
                || aHidEvent.IsButtonUp
StephaneLenclud@246
   189
                || !aHidEvent.IsValid
StephaneLenclud@246
   190
                || aHidEvent.IsBackground
StephaneLenclud@246
   191
                || aHidEvent.IsRepeat
StephaneLenclud@246
   192
                || aHidEvent.IsStray)
StephaneLenclud@246
   193
            {
StephaneLenclud@246
   194
                return;
StephaneLenclud@246
   195
            }
StephaneLenclud@246
   196
StephaneLenclud@246
   197
            PrivateCopy(aHidEvent);
StephaneLenclud@246
   198
            //
StephaneLenclud@246
   199
StephaneLenclud@246
   200
            //Tell observer the object itself changed
StephaneLenclud@246
   201
            OnPropertyChanged("Brief");
StephaneLenclud@246
   202
        }
StephaneLenclud@246
   203
StephaneLenclud@246
   204
        /// <summary>
StephaneLenclud@246
   205
        /// 
StephaneLenclud@246
   206
        /// </summary>
StephaneLenclud@246
   207
        /// <param name="aHidEvent"></param>
StephaneLenclud@246
   208
        public void Copy(Hid.Event aHidEvent)
StephaneLenclud@246
   209
        {
StephaneLenclud@246
   210
            PrivateCopy(aHidEvent);
StephaneLenclud@246
   211
            //We need the key up/down too here
StephaneLenclud@246
   212
            IsKeyUp = aHidEvent.IsButtonUp;
StephaneLenclud@246
   213
        }
StephaneLenclud@246
   214
StephaneLenclud@246
   215
        /// <summary>
StephaneLenclud@246
   216
        /// 
StephaneLenclud@246
   217
        /// </summary>
StephaneLenclud@246
   218
        /// <param name="aHidEvent"></param>
StephaneLenclud@246
   219
        private void PrivateCopy(Hid.Event aHidEvent)
StephaneLenclud@246
   220
        {
StephaneLenclud@246
   221
            //Copy for scan
StephaneLenclud@246
   222
            UsagePage = aHidEvent.UsagePage;
StephaneLenclud@246
   223
            UsageCollection = aHidEvent.UsageCollection;
StephaneLenclud@246
   224
            IsGeneric = aHidEvent.IsGeneric;
StephaneLenclud@246
   225
            IsKeyboard = aHidEvent.IsKeyboard;
StephaneLenclud@246
   226
            IsMouse = aHidEvent.IsMouse;
StephaneLenclud@246
   227
StephaneLenclud@246
   228
            if (IsGeneric)
StephaneLenclud@246
   229
            {
StephaneLenclud@246
   230
                if (aHidEvent.Usages.Count > 0)
StephaneLenclud@246
   231
                {
StephaneLenclud@246
   232
                    Usage = aHidEvent.Usages[0];
StephaneLenclud@246
   233
                    PersistedBrief = aHidEvent.UsageName(0);
StephaneLenclud@246
   234
                }
StephaneLenclud@246
   235
StephaneLenclud@246
   236
                Key = Keys.None;
StephaneLenclud@246
   237
                HasModifierAlt = false;
StephaneLenclud@246
   238
                HasModifierControl = false;
StephaneLenclud@246
   239
                HasModifierShift = false;
StephaneLenclud@246
   240
                HasModifierWindows = false;
StephaneLenclud@246
   241
            }
StephaneLenclud@246
   242
            else if (IsKeyboard)
StephaneLenclud@246
   243
            {
StephaneLenclud@246
   244
                Usage = 0;
StephaneLenclud@246
   245
                Key = aHidEvent.VirtualKey;
StephaneLenclud@246
   246
                HasModifierAlt = aHidEvent.HasModifierAlt;
StephaneLenclud@246
   247
                HasModifierControl = aHidEvent.HasModifierControl;
StephaneLenclud@246
   248
                HasModifierShift = aHidEvent.HasModifierShift;
StephaneLenclud@246
   249
                HasModifierWindows = aHidEvent.HasModifierWindows;
StephaneLenclud@246
   250
            }
StephaneLenclud@246
   251
StephaneLenclud@246
   252
        }
StephaneLenclud@246
   253
StephaneLenclud@246
   254
StephaneLenclud@244
   255
    }
StephaneLenclud@244
   256
}