I am currently working on an app that simulates the d-pad (up, down, left, and right). The problem is that I can't figure out how to send those key presses to other applications.
Currently, I am using Instrumentation and it works perfectly fine in the activity of my app.
Here is the snippet of the arrow key left code:
new Thread(new Runnable() {
#Override
public void run() {
new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT);
}
}).start();
The problem with this implementation is that due to an android security feature, I cannot send key presses to other applications.
After searching up different ways to injecting key presses, I came across this article. However, after trying the example code, it doesn't seem to work. I poked around and found out that android doesn't seem to have an input device for the keyboard. When I scan for different input devices, I get stuff such as orientation sensor and etc. Here is a pic of all the input devices on my phone (T-959 Galaxy S).
As you can see, none of these devices is a keyboard. I have a feeling that this is because my phone does not have a physical keyboard. (cypress is the capacitive keys, aries is the volume buttons, and mxt224 is the touchscreen controller)
After a bit more of research, I found that the accessibility api in android has limit capabilities for injecting events, but those are only very basic things such as switching view focus and etc (AccessibilityNodeInfo). However, I have a feeling that I might be able to extend it to support arrow key presses.
So currently, I am very confused on where to go. Has anyone ever done anything like this before? It seems that the simple injection of arrow keys (nothing else) is very frustrating due to the security features in Android. All suggestions are welcomed.
EDIT: I might not have been very clear, but I am fine with building this for rooted phones only. Only requirement is that this app has to run standalone on the phone without being hooked up to any external keyboards via otg or the computer via adb.
I wanted to simulate DPAD key press events few days back and I did find a solution which has its own limitation.
Arrow key press can be simulated using adb commandinput keyevent <keycode>.
So if you want to simulate a DPAD LEFT key press then use the adb commandadb input keyevent 21.
To reuse this bit of code and scale it forward, I created a standalone swing application where i select the device to which i need to send the key events and I can use the existing keyboard of my computer to type in the keys.[I map the keyboard key codes to android key codes].
I am further scaling the application by including remote and game pad buttons, once done, i'll upload the project on git.
Hope this helps.
IF you intention is to create an application that works on non rooted phones perhaps your best bet is to implement a new InputMethod using http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html
Related
My android watch has a single hardware (physical) button on the side. Is it possible to program a listener for this button, so that it can be used as an additional control in my program?
I don't believe this is possible. The usual way to detect a standard button press (like Back) in Android is with an onKeyDown listener, and this doesn't fire in an Activity on Wear when the stem button is pressed (tested on both Wear 1.5 and the 2.0 dev preview).
It's always iffy to assert something is categorically not possible, because there's always the chance that someone will find a clever hack to make it happen. And I wouldn't mind being proved wrong. But until such a hack comes along, I'm going to say no, it's not.
I've got new mobile from ebay, Blackview bv6000, which has 3 special physical buttons
Camera
Sos
Ptt
Then i need to assign those buttons to run my app and some functions
I got stock rom from Blackview forum, after i extract system.img file and found
\usr\keylayout\Generic.kl
which contains list of key function that i need
key 212 CAMERA
...
#[AGOLD][2015.04.08]Fengjun merge
key 249 FUNC1
key 250 FUNC2
#[AGOLD]End
The question is how can i assign that key to run my app or function in my app?
Edit
More info:
I want to catch button event in home screen or in other app or even the screen is off
Look into KeyEvent which is shown in an functioning example in this answer. For the specific keys, check out https://developer.android.com/reference/android/view/KeyEvent.html
For example, KEYCODE_CAMERA for the camera button.
In my company we have been searching for this for a while and finally I have an answer.
We have recently contacted with the BV6000 main provider and they told me that those buttons can only be overrided by customizing the hardware specifically for your app and that they require a 500+ phones commercial order to start the procediment of customizing something.
They won't move a finger neither give any documentation for less than that :-/
I am developing an accessibility based application which lets users use the application using a bluetooth keyboard, hands-free. What I need now is for the users to be able to zoom in and out, in the application(and possibly the whole android system) using certain key-combo(say Ctrl+Alt+?).
The zoom should work something like the Magnification Gesture feature of the android accessibility settings.The magnification on that works on triple-tap to activate(which I want to activate by a certain key-combo) and pan around using two finger swipe( Which probably would be the direction keys from keyboard).
The only thing I have found even remotely related to doing something like this is the Android Accesibility Service. But I dont think it let's me get keyboard key-combo and then zoom-in the screen.
Is there any way we can do this in Android? Do I need to go AOSP and make my own Android OS version probably? Please help!
I want to prevent users from leaving the application (client requirement, not a mass public application).
But the IME allows the user to reach settings by pressing some touch. How can I block this without developing my own keyboard ?
As far as I understand from Google documentation, there is not much I can do because the IME is the first component to receive the screen inputs and it is the IME which decides what inputs my application receives.
Any pointer appreciated.
There's really no way to prevent somebody from being able to leave your app without a custom rom. However, Android 5.0 Lollipop allows device owners to pin the screen. For example, conferences that hand out tablets to their attendees are using the feature to restrict tablet usage to the conference app.
Here's a good write up to the feature:
http://www.androidpolice.com/2014/10/17/lollipop-feature-spotlight-screen-pinning-allows-you-to-lock-your-device-to-a-single-app-before-handing-it-to-a-friend/
Alternatively you could try one of the various launchers on the Play Store that use alternative locking strategies, but in my experience there's often a way around them unless they simply lock the device completely.
I'm using a bluetooth keyboard with an android tablet and i've found that there's some shortcut avaible from a qwerty keyboard. I want to generate some of them from the code. I'm able to trigger the majority of all the key combination but I'm not able to trigger a winKey (The windows logo key on a qwerty). I've basically try every KEY CODE from android and non of them want to work.
If any one as a little hint on that subject that would be appreciated.