Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen to any keyboard on the whole application?

We are trying to build android activity that listen to any key press (either character or command) something like custom EditText.

  1. Is there something like keyboard hook in Windows OS available on Android?
  2. Is it possible to listen for all key pressed for all controls (EditText and other controls)?
  3. Could this be achieved by an activity that runs in the background?

EDIT

As for security, we want to get keyboard events only for our app activity, ex: when our activity is shown and focused.

like image 977
Sameer H. Ibra Avatar asked Dec 31 '13 09:12

Sameer H. Ibra


3 Answers

The Activity's onKeyDown / up / etc method will get called if no View consumes the event.

When Views do consume the events, you will need either:

  1. a custom version of each relevant class that forwards the key presses up to the activity.
  2. add a text change listener or key listener to all TextViews in the view hierarchy (could use a recursive method to step through the entire hierarchy)
like image 82
FunkTheMonk Avatar answered Nov 14 '22 14:11

FunkTheMonk


You can't listen for EditText keys from background process, as it is bounded to Keyboard activity that manipulate that text. It will be security valuation as it will not only allow you to listen to keys in your own application but for all others, including sensitive data like user passwords.

like image 21
Ilya Gazman Avatar answered Nov 14 '22 12:11

Ilya Gazman


Handling Keyboard Actions
http://developer.android.com/training/keyboard-input/commands.html

An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.

http://developer.android.com/guide/topics/ui/ui-events.html#EventListeners

like image 1
Dharmendra Avatar answered Nov 14 '22 12:11

Dharmendra