7

I need to implement certain functions only when the keyboard is attached to the surface. Is there a way I can detect when the surface keyboard is attached or removed ?

I tried this code on Surface:

function getKeyboardCapabilities()
{   
   var keyboardCapabilities = new Windows.Devices.Input.KeyboardCapabilities();
   console.log(keyboardCapabilities.keyboardPresent);

}

The result was always '1' even when the keyboard was not connected.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Stefania
  • 791
  • 1
  • 6
  • 17
  • My understanding is that you can know whether one has been attached, but not whether it currently is attached. Look at `KeyboardCapabilities.KeyboardPresent` – WiredPrairie Nov 21 '13 at 15:40
  • @WiredPrairie I tried using `KeyboardCapabilities.keyboardPresent` on a computer with a physical keyboard and the result was '1' which is good. However the same code on a Surface always returned '1' even when the keyboard was not attached. – Stefania Nov 26 '13 at 14:13
  • Unfortunately, I think that's just how it works. Surface always reports true if it's been attached at some point. – WiredPrairie Nov 26 '13 at 14:36
  • 1
    Have any magical solutions appeared in the last 4 years? – Felix May 09 '17 at 17:32

2 Answers2

2

I used this code to identify when a keyboard is connected to a Surface:

var keyboardWatcher = (function () {
    // private
    var keyboardState = false;

    var watcher = Windows.Devices.Enumeration.DeviceInformation.createWatcher();
    watcher.addEventListener("added", function (devUpdate) {
    // GUID_DEVINTERFACE_KEYBOARD
        if ((devUpdate.id.indexOf('{884b96c3-56ef-11d1-bc8c-00a0c91405dd}') != -1) && (devUpdate.id.indexOf('MSHW0007') == -1)    ) {
            if (devUpdate.properties['System.Devices.InterfaceEnabled'] == true) {
                // keyboard is connected 
                keyboardState = true;
            }
        }
    });
    watcher.addEventListener("updated", function (devUpdate) {

        if (devUpdate.id.indexOf('{884b96c3-56ef-11d1-bc8c-00a0c91405dd}') != -1) {
            if (devUpdate.properties['System.Devices.InterfaceEnabled']) {
                // keyboard is connected 
                keyboardState = true;
            }
            else {
                // keyboard disconnected
                keyboardState = false;
            }
        }
    });

    watcher.start();

    // public
    return {
        isAttached: function () {
            return keyboardState;
        }
    }

})(); 

Then call KeyboardWatcher.isAttached() whenever you need to check the status of the keyboard.

Stefania
  • 791
  • 1
  • 6
  • 17
  • Tried this solution on the surface pro and unfortunately when the OnScreenKeyboard pops up it sets the flag to `true` – JKennedy Aug 22 '19 at 13:52
0

I could not find a good way to detect if a keyboard is attached so instead I detect if I am in tablet mode or desktop mode.

        bool bIsDesktop = false;

        var uiMode = UIViewSettings.GetForCurrentView().UserInteractionMode;
        if (uiMode == Windows.UI.ViewManagement.UserInteractionMode.Mouse)          // Typical of Desktop
            bIsDesktop = true;

Note the other possible value of uiMode is Windows.UI.ViewManagement.UserInteractionMode.Touch.

Gary Shaw
  • 51
  • 2
  • For a surface pro with no keyboard attached this returns true, so this solution doesnt work in this case – JKennedy Aug 22 '19 at 13:36