Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if the surface keyboard is attached?

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.

like image 627
Stefania Avatar asked Nov 21 '13 15:11

Stefania


People also ask

Can you use the Surface keyboard unattached?

No. The keyboard only works while the screen is attached to it.

Are surface laptop keyboards detachable?

Best known for innovative, tablet-like designs, Microsoft has expanded its Surface line over the years and now offers a full family of computers, from a standard laptop to a giant desktop. It's hard to find a PC maker today that doesn't ape the Surface's kickstand-packing, detachable-keyboard-rocking design.

Is keyboard detachable from Surface Pro?

Surface Pro 8 combines the power of a laptop with the flexibility of a tablet, and every angle in between, with the iconic Kickstand and detachable Keyboard with built-in Slim Pen storage and charging.


1 Answers

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.

like image 93
Stefania Avatar answered Sep 23 '22 00:09

Stefania