7

只有当键盘连接到表面时,我才需要实现某些功能。有没有办法可以检测到 Surface 键盘何时连接或移除?

我在 Surface 上试过这段代码:

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

}

即使未连接键盘,结果也始终为“1”。

4

2 回答 2

2

我使用此代码来识别键盘何时连接到 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;
        }
    }

})(); 

然后KeyboardWatcher.isAttached()在需要检查键盘状态时调用。

于 2013-12-02T14:49:01.437 回答
0

我找不到检测是否连接键盘的好方法,所以我检测我是处于平板电脑模式还是桌面模式。

        bool bIsDesktop = false;

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

请注意,uiMode 的另一个可能值是 Windows.UI.ViewManagement.UserInteractionMode.Touch。

于 2016-01-16T07:06:27.467 回答