How to add keyboard shortcuts to your Android app

With a simple keyboard accessory, a tablet becomes a decent approximation of a netbook (which might be part of the reason they seem to be eating into that market). The problem is that not many Android developers make use of keyboard shortcuts in their apps, and this got me wondering:

  1. Does Android allow for keyboard shortcuts?
  2. Is it hard to code into your app?

The answers are:

  1. Yes.
  2. No.

Elaborating on question 2: In an Android activity, there is an onKeyShortcut method that one can override to provide global keyboard shortcuts for that Activity. Below follows a small example:

Code for keyboard shortcuts in an Android app

 

The above example only takes care of a single shortcut for Ctrl+B, but more cases can be added for more shortcuts. The event paramater can also be used to check if other meta-keys are pressed, which allows for a wide variety of keyboard shortcuts.

This shows that it’s not particularly hard to add keyboard shortcuts to your app, and they really are useful for your (slightly) more advanced users.

Advertisements

About HawkiesZA

about.me/hawkiesza
This entry was posted in Android, Development, How To and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s