Home » Android » android – Simulating opening/closing the hardware keyboard in emulator

android – Simulating opening/closing the hardware keyboard in emulator

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is there a way to simulate opening/closing the ‘hardware’ keyboard in the emulator?
I have to test some changes that I capture using getResources().getConfiguration().keyboardHidden and this would help a fair lot 🙂

Thank you in advance!

How to&Answers:

To add to Samuh’s explanation (which I didn’t know — good to know that I can still simulate opening/closing the hardware keyboard by changing orientation), you can test the difference between landscape with no keyboard and landscape with keyboard open, by changing the hardware configuration of the emulator’s AVD: The AVD can be configured to either have a hardware keyboard or not. If your emulator has no hardware keyboard, the soft keyboard appears as you would expect, and going to landscape mode should appear as it would in landscape mode on a phone with no keyboard.

To configure the emulator AVD to act as if it has no hardware keyboard, in the AVD & SDK Manager tool, click “New” to add a new AVD, select your desired SDK level and other options, and under the Hardware section, add a new property for “Keyboard support”, and set it to “No”. In the AVD emulator “Details” dialog, this will show up as “hw.keyboard = no”.

Note: you can also modify an existing AVD to toggle its keyboard support by:

  1. Open the AVD Manager
  2. Select the AVD you want to edit
  3. Click on Edit… on the right
  4. In the Hardware section, click on New… on the right
  5. In the Property combo box select Keyboard support and click OK
  6. Again in the Hardware section, change the Keyboard support value column to “no”
  7. Finally click Edit AVD and exit the Manager.

Alternatively, you can also edit the <avd path>/avd/<AVD device name>.avd/config.ini file, and changing the hw.keyboard entry to yes or no. On Mac and Linux, is generally ~/.android/, and on Windows /Users/<user-name>/.android/.

Based on Samuh’s answer, that means you can check the behavior of the phone being in landscape mode with keyboard open (emulator has a hardware keyboard configured), or in landscape mode with no keyboard (emulator is configured with no keyboard). Two minor frustrations with this are 1) you can’t necessarily emulate how it will look in landscape mode on a device that has a hardware keyboard, but the keyboard is hidden (if I understand correctly), and 2) you would have to run two separate emulators or at least two AVDs booted separately, in order to test those two different cases.

Answer:

I had this question once(about simulating keyboard slide-out/in) and was expecting to find something in the Simulating Hardware events using Emulator console section of the Emulator’s official documentation but unfortunately I couldn’t. I thought it would have been a lot easier if sliding a keyboard in/ out and its emulation were just a hardware signal to be sent to the kernel.

I had to then resort to the indirect method of changing the device orientation by depressing the PageUp button on the Numpad. When you change the orientation of the device like this and print the value of expression (getResources().getConfiguration().hardKeyboardHidden == config.HARDKEYBOARDHIDDEN_YES) you get different values for portrait and landscape. So changing the emulator orientation is somehow related to making the hardkeyboard slide out(not hidden) and slide in(hidden).

See if that works in your case…