Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to program Android to act as physical USB keyboard?

What I really want to know is whether it is a hardware problem, or a software problem. Could I plug my android phone into a computer via USB and have it act as a hardware keyboard. I do not want to install anything on the computer, I want android to behave like the standard hardware.


Edit: Clarification I want to write a program/library for android that enables the device to fully emulate an ordinary keyboard, so that the operating system reports it as a standard keyboard device, and it would work in the BIOS or anywhere else that a keyboard works.

like image 858
Billy Moon Avatar asked Mar 21 '12 13:03

Billy Moon


People also ask

How do I turn my Android into a USB keyboard?

Install MyPhoneExplorer on both a Windows PC and Android phone. Connect by USB. Enable the MyPhoneExplorer keyboard installed as an input method. In the Extras menu on the PC mirror the phone screen, then you can type on the laptop to the phone.

Can I use my phone as a wired keyboard for my computer?

Yes at least for android phones as long as they have USB OTG support.

Can you use physical keyboard on phone?

On your phone, go to Settings and then the System page. Scroll down until you find the section entitled “OTG storage,” and turn on the option. When you connect the USB OTG cable to the physical keyboard, you will be able to use the keyboard to type on your phone.


4 Answers

I've modified kernel on Nexus 7 to act like standard HID keyboard and mouse, without losing MTP/ADB/other USB functionality.

You can use usb-gadget-test commandline utility to send keystrokes and mouse movements to your PC. I want to create a remote admin app later, which will send key events and receive video from camera.

Kernel patch, binaries and instructions: https://github.com/pelya/android-keyboard-gadget

Edit: I've published a proper app to Google Play, if your Nexus 7 is rooted you can flash kernel right from the app, and send keypresses with it.

like image 83
pelya Avatar answered Oct 19 '22 13:10

pelya


Most USB keyboards need drivers to run. Any keyboard functionality (non-standard buttons) beyond the capabilities of the standard HID drivers will need to install some software on the computer.

That being said, It may be possible to use Android's USB capabilities, as well as writing a custom driver if default HID is not sufficient, to achieve your goal. It is likely a very non-trivial undertaking.

Edit: I think KristopherMicinski is right that the level of control you get with the stock Android USB API is inadequate for this purpose. His two solutions of modifying the firmware to communicate using HID standards, as well as a hardware middleman that translates from the Android Accessory protocol to HID both seem valid to me. If installing drivers on the computer is out of the question, these may be the only two options.

However, if you're open to installing a driver for this behavior, It should be possible to write a custom driver that can handle Android USB protocol, and correctly translate to the correct calls/interrupts for keyboard functionality. If memory serves, every peripheral keyboard I've used in the last 10 years has needed to install a driver for full functionality, so this may not be considered non-standard behavior. (The though just occurs that this approach will only allow the device to function as a keyboard inside windows, not during the boot process)

like image 42
Chris Bye Avatar answered Oct 19 '22 13:10

Chris Bye


Looks like someone finally did it, it is a tiny bit ugly - but here it is:

http://forum.xda-developers.com/showthread.php?t=1871281

It involves some kernel recompiling, and a bit of editing, and you loose partial functionality (the MDC?) .. but it's done.

Personally though, now that I see the "true cost", I would probably put together a little adapter on a Teency or something - assuming that Android can talk to serial devices via USB. But that's based on the fact that I have a samsung, and would require a special cable to make a USB connection anyway - no extra pain to have a little device on the end, if I have to carry the damn cable around anyway.

like image 11
Orwellophile Avatar answered Oct 19 '22 11:10

Orwellophile


Seems someone have done it by patching the kernel. I just came across a paper titled "Exploiting Smart-Phone USB Connectivity For Fun And Profit" by Angelos Stavrou, Zhaohui Wang, Computer Science Department George Mason University, Fairfax, VA. (available freely by googling the above title). Here the two researchers are investigating the possibility of a compromised android device controlling the attached PC by having the android device presenting itself as an HID device (keyboard). As a proof of concept, it seems that they have successfully patched a kernel doing exactly what you want. They didn't provide detailed steps but anyway I just quote what they said they've done:

.....we developed a special USB gadget driver in addition to existing USB composite interface on the Android Linux kernel using the USB Gadget API for Linux[4]. The UGAL framework helped us implement a simple USB Human Interface Driver (HID) functionality (i.e. device driver) and the glue code between the various kernel APIs. Using the code provided in: “drivers/usb/gadget/composite.c”, we created our own gadget driver as an additional composite USB interface. This driver simulates a USB keyboard device. We can also simulate a USB mouse device sending pre-programmed input command to the desktop system. Therefore, it is straightforward to pose as a normal USB mouse or keyboard device and send predefined command stealthily to simulate malicious interactive user activities. To verify this functionality, in our controlled experiments, we send keycode sequences to perform non-fatal operations and show how such a manipulated device can cause damages In particular, we simulated a Dell USB keyboard (vendorID=413C, productID=2105) sending ”CTRL+ESC” key combination and ”U” and ”Enter” key sequence to reboot the machine. Notice that this only requires USB connection and can gain the ”current user” privilege on the desktop system. With the additional local or remote exploit sent as payload, the malware can escalate the privilege and gain full access of the desktop system.

like image 5
JavaMan Avatar answered Oct 19 '22 11:10

JavaMan