簡體   English   中英

如何以編程方式檢測 chromebook 模式

[英]How to detect chromebook mode programmatically

這個問題是這個#3的一部分

有些 Chromebook 是筆記本電腦,我們也可以將其轉換為平板電腦模式。 這里查看圖片

所以我的問題是如何以編程方式檢測 Chromebook 的模式(筆記本電腦或平板電腦)。 為此,我這樣做了,但這僅適用於Lenovo Flex11 ,在其他 Chromebook 中不起作用

context.getResources().getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES;

如果此條件返回 true,則表示 Chromebook 處於平板電腦模式,否則處於筆記本電腦模式

我需要檢查這一點,因為如果 Chromebook 處於筆記本電腦模式,那么我必須僅在特定活動中顯示預測欄。 如果是平板模式,會出現軟鍵盤,預測欄由InputMethodService的候選視圖管理

我們使用兩種不同事物的組合。 首先是我們使用以下代碼片段檢測設備是否處於桌面模式:

fun isDesktopMode() : Boolean {
   var hasMouse = false
   val hasKeyboard = resources.configuration.keyboard == KEYBOARD_QWERTY
   val isKeyboardUseable = resources.configuration.hardKeyboardHidden == HARDKEYBOARDHIDDEN_NO

   // Check each input device to see if it is a mouse
   val inputManager = getSystemService(Context.INPUT_SERVICE) as InputManager
   for (deviceId in inputManager.inputDeviceIds) {
       val sourceMask = inputManager.getInputDevice(deviceId).sources
       if ((sourceMask or SOURCE_MOUSE == sourceMask) ||
           (sourceMask or SOURCE_TOUCHPAD == sourceMask) ||
           (sourceMask or SOURCE_TRACKBALL == sourceMask)) {
               hasMouse = true
       }
   }

   return hasMouse && hasKeyboard && isKeyboardUseable
}

為了檢測鍵盤是否已插入和/或可用,我們使用以下偵聽器來偵聽鍵盤是否已激活:

val inputManager = getSystemService(Context.INPUT_SERVICE) as InputManager
val inputDeviceListener = object: InputManager.InputDeviceListener {
   override fun onInputDeviceRemoved(deviceId: Int) {
       // Be careful checking device here as it is no longer attached to the system
   }

   override fun onInputDeviceAdded(deviceId: Int) {
       // If you want to learn more about what has been attached, check the InputDevice
       // using the id.
       val sourceMask = inputManager.getInputDevice(deviceId).sources
       if (sourceMask or SOURCE_KEYBOARD == sourceMask) {
           //Keyboard has been Added
           append_to_log("A keyboard has been added.")
       }
   }

   override fun onInputDeviceChanged(deviceId: Int) {
       // Best practice is to check for what you care about when things change (ie. are
       // there any keyboards/mice still attached and usable. Users may have multiple
       // devices attached (integrated keyboard a and bluetooth keyboard) and
       // removing one does not mean the other is no longer available.
       isInDesktopMode = isDesktopMode()
   }
}
inputManager.registerInputDeviceListener(inputDeviceListener, null)

這篇文章在 Apache 2.0 下獲得許可。

https://developers.google.com/open-source/devplat

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM