[Nexus 4 General] IAmAN00bie:
REQUIREMENTS

ROOT ACCESS

SINGLE-BUTTON HEADSET

ANDROID 4.1+

PREFACE

TESTED ON THE NEXUS 4 RUNNING 4.2

If you often listen to music using a single-button headset (the kind that many Samsung devices tend to ship with), then you should know that Google lets you control the headset button. By default, you can:

Play/Pause (Single Press)

Skip track (Double Press)

Launch Voice Search (Single-click Long Press)

Pretty cool, right? So what's the deal, then?

The issue is you CANNOT customize how you want the system to respond to your button input.

What if you want to long press to increase volume?

What if you want to triple click to go to the previous track?

What if you want to double click and hold to decrease volume?

What if you want to launch an app besides Google Voice Search (I personally have bound utter! in place of Google's search!)?

Nope, too bad.

Well, there is a workaround for this.

How does it work?

In /system/usr/keylayout, two files called Generic.kl and hs_detect.kl reside where you can modify the keymapping of the HEADSETHOOK input to an alternate key that is normally found on a 3-button headset, specifically MEDIA_NEXT. Then, you can modify how the system responds to MEDIA_NEXT by using a headset control app.

TO FIX:

**NOTE: ITEMS SURROUNDED BY A BLUE BOX MEAN IT IS THE ORIGINAL VALUE WHILE ITEMS SURROUNDED BY A RED BOX MEANS IT IS THE NEW VALUE**

1) Install the Keytest App found here: https://github.com/chrisboyle/keytest/downloads

2) Once inside the app, press the headset button. On your screen, it should read as follows:

keyCode=KEYCODE_HEATSETHOOK
scanCode=226

as shown in the following picture: http://i.imgur.com/dogxm.png

3) Using a root-enabled file explorer app (I personally use Solid Explorer), go to /system/usr/keylayout and make a backup of Generic.kl and hs_detect.kl (copying them to your internal storage, for example /sdcard/headset).

4) You will modify these two files on two different lines. The folder should look like this: http://i.imgur.com/ShpCv.png

Generic.kl:

ORIGINAL: http://i.imgur.com/dzmPy.png

AND

http://i.imgur.com/bdRRz.png

NEW: http://i.imgur.com/ZddZT.png

AND

http://i.imgur.com/PPAZk.png

hs_detect.kl:

ORIGINAL: http://i.imgur.com/dVaXK.png

NEW: http://i.imgur.com/Deh9r.png NOTE: I MANUALLY ADDED THE KEY 163 LINE IN

Simple right? You are just flipping the two values essentially.

5) Save the changes. Reboot.

6) Open the Keytest app again. Press the headset button. On your screen, it should read as follows:

keyCode=KEYCODE_MEDIA_NEXT
scanCode=226

as shown in the following picture: http://i.imgur.com/2BSHM.png

7) Now all you need to do is use an app to configure the MEDIA_NEXT key to your liking.

MEDIA_NEXT is a key typically found on 3-button headsets, symbolized by the "Right" key. An app like Headset Button Controller or Headset Droid should do the trick. Both are paid apps, but there are trial versions available. If there are any free alternatives that can do the trick, let me know and I'll add it in! (I am NOT in any way affiliated with the app developers!)

LIMITATIONS

1)A ROM update will wipe/reset the files.

1a) How can you avoid this? Pretty simple, actually! Just keep a backup of Generic.kl and hs_detect.kl after you make the changes to them, and inject them into the zip file before you flash the ROM. You can also use the following script to do this which needs to be run as root, given that you have the two files backed up in /sdcard/headset

mount -o rw,remount /system

cp -f /sdcard/headset/* /system/usr/keylayout

chmod 644 /system/usr/keylayout/*

mount -o ro,remount /system

2) An actual or simulated "MEDIA_NEXT" input will now not be recognized as a MEDIA_NEXT. This will affect, for example, 3-button headsets or tasker tasks that simulate a MEDIA_NEXT input. Also: ROMs with the long-press to skip tracks will no longer skip track on holding volume-up, as this is no longer recognized as a MEDIA_NEXT input.

2a) How can you avoid this? You can instead use a different key to be replaced rather than MEDIA_NEXT, if you so desire. The work-up will be the exact same, only the values you swap will be different.

CREDITS

Kenkiller: http://xdaforums.com/showpost.php?p=32999600&postcount=38

Headset Button Controller Dev

Headset Droid Dev

Redditor muzeofmobo for the script