How do I swap (reassign) the capacitative hardware keys MENU and BACK on a Samsung Galaxy S3 Neo? I am on Cyanogenmod 12.1 on a Samsung Galaxy S3 Neo.

I found various answers on how to reassign hardware keys on Android devices, but they were always specifically for other devices and did not explain the rationale behind them, so that I could apply them for my particular device, without a long series of time intensive trial and error with a lot of config file edits and rebooting inbetween.
Instructions:


Requires root!

a) Under Cyanogenmod Root activation itself requires the developer menu: Settings > About > Tap "Build number" multiple times until a toast message tells you that the developer menu got activated.
b) Settings > Developer > Root access > Set to: Apps &amp; ADB.
Remark: Apps alone should suffice too, but maybe you want to edit the config file via USB cable from your Desktop.

Modify /system/usr/keylayout/sec_touchkey.kl which requires root:

a) File Manager (ships with Cyanogenmod) > Settings > General > Access mode > Extended
b) Now open that file and exchange 139 with 158 and 158 with 139.
c) Save. You will be prompted for root access. Permit it. You should get feedback that the file was saved. Close the app.
Remark: A lot of tutorials instruct to alter /system/usr/keylayout/Generic.kl, which on the S3 Neo had no effect. The official documentation makes clear why, it explicitely tells "Do not modify the generic key layout!" and gives applicable instructions for "Capacitive Buttons".

Reboot. Test if your keys work as intended.

For security reasons disable root (See: 1b) if you don't have good reasons to keep it on.
To undo your modification restore the keycodes in sec_touchkey.kl as they were before (See: 2b).