Exemple #1
0
 public void ReleaseKey(uint ps2Set1keyScanCode)
 {
     try
     {
         ChangeKeyState(KeyEvent.KeyBreak, HidHelper.GetHidUsageFromPs2Set1(ps2Set1keyScanCode));
     }
     catch (Exception e)
     {
         Debug.WriteLine("Failed to change the key state due to: " + e.Message);
     }
 }
Exemple #2
0
        private void ChangeKeyState(KeyEvent keyEvent, byte hidUsageScanCode)
        {
            lock (m_lock)
            {
                if (!m_initializationFinished)
                {
                    return;
                }

                if (keyEvent == KeyEvent.KeyMake)
                {
                    if (HidHelper.IsMofifierKey(hidUsageScanCode))
                    {
                        Debug.WriteLine("Modifier key depressed: " + hidUsageScanCode);
                        m_currentlyDepressedModifierKeys.Add(hidUsageScanCode);
                    }
                    else
                    {
                        Debug.WriteLine("Key depressed: " + hidUsageScanCode);
                        m_currentlyDepressedKeys.Add(hidUsageScanCode);
                    }
                }
                else
                {
                    if (HidHelper.IsMofifierKey(hidUsageScanCode))
                    {
                        Debug.WriteLine("Modifier key released: " + hidUsageScanCode);
                        m_currentlyDepressedModifierKeys.Remove(hidUsageScanCode);
                    }
                    else
                    {
                        Debug.WriteLine("Key released: " + hidUsageScanCode);
                        m_currentlyDepressedKeys.Remove(hidUsageScanCode);
                    }
                }

                if (m_hidKeyboardReport.SubscribedClients.Count == 0)
                {
                    Debug.WriteLine("No clients are currently subscribed to the keyboard report.");
                    return;
                }

                var reportValue = new byte[c_sizeOfKeyboardReportDataInBytes];

                // The first byte of the report data is a modifier key bitfield.
                reportValue[0] = 0x0;
                foreach (var modifierKeyPressedScanCode in m_currentlyDepressedModifierKeys)
                {
                    reportValue[0] |= HidHelper.GetFlagOfModifierKey(modifierKeyPressedScanCode);
                }

                // The second byte up to the last byte represent one key per byte.
                int reportIndex = 1;
                foreach (var keyPressedScanCode in m_currentlyDepressedKeys)
                {
                    if (reportIndex >= reportValue.Length)
                    {
                        Debug.WriteLine("Too many keys currently depressed to fit into the report data. Truncating.");
                        break;
                    }

                    reportValue[reportIndex] = keyPressedScanCode;
                    reportIndex++;
                }

                if (!reportValue.SequenceEqual(m_lastSentKeyboardReportValue))
                {
                    Debug.WriteLine("Sending keyboard report value notification with data: " + GetStringFromBuffer(reportValue));
                    reportValue.CopyTo(m_lastSentKeyboardReportValue, 0);

                    // Waiting for this operation to complete is no longer necessary since now ordering of notifications
                    // is guaranteed for each client. Not waiting for it to complete reduces delays and lags.
                    // Note that doing this makes us unable to know if the notification failed to be sent.
                    var asyncOp = m_hidKeyboardReport.NotifyValueAsync(reportValue.AsBuffer());
                }
            }
        }