android - Share onKeyDown between Preferences and Activity? -
my app allows users change hard keys on it. can choose each key in app preferences , on onkeydown fire action selected user.
i noticed many of test functionality, first set hard key , immediatelly press key corroborate if change in effect. right onkeydown on mainactivity aren't getting right feedback.
for reason want reuse onkeydown in both mainactivity , preferenceactivity. tried extend baseactivity can't work both preferenceactivity , regular one. there way through innheritance? how can it?
you may not able via inheritance since preferenceactivity inherits activity you'd have write 2 classes anyway (which defeats purpose).
you alternatively use mapping transforms keydown event 1 user has set.
public class downkeymapper { private static map<integer, integer> mkeycodemap; public static void init(map<integer, integer> pkeymap) { // init here mappings preferences mkeycodemap = pkeymap; } public static void mapkey(int pfromkeycode, int ptokeycode) { mkeycodemap.put(pfromkeycode, ptokeycode); } public static int getmappedkey(int pkeycode) { if (mkeycodemap.containskey(pkeycode)) { return mkeycodemap.get(mkeycodemap); } else { return pkeycode; } } }
so in onkeydown you'd use:
@override public boolean onkeydown(int keycode, keyevent event) { keycode = downkeymapper.getmappedkey(keycode); return super.onkeydown(keycode, event); }
hope helps!
Comments
Post a Comment