Short-Press, double-click, long-press events for custom keystrokes in the Android system

Source: Internet
Author: User

This problem is encountered in the project:
Since the keys in the system have been redefined or new keys are added, it is necessary to decompose the key events (KeyEvent) in the app layer, simulating the Android system practice and decomposing the keyevent into:
1, click the event: is the ordinary key click;
2, double-click the event: 500ms within the same keystroke two times;
3, Long press event: The same key long press more than 1000MS (the system long press event is 500ms);
4, the combination of keys: More than two keys at the same time hold;

The keyevent can come from the activity, view sub-class Dispatchkeyevent method, can also be our custom interface, or we can send the broadcast, according to project needs;

About the principles of each event:
1. Double-click the event: Start a timed (500ms) thread message in the up event of each click, using the Handler.postdelayed () method.
2. Long press event: Start a timed (1000ms) thread message in the down event of each click, using the Handler.postdelayed () method, note: start at repeatcount==0;
3. Combination key: Record the state of each key with variable, then make judgment;

The specific code is as follows: Java code

Package Com.jerome.util;import Android.content.context;import Android.os.handler;import android.util.Log;import Android.view.keyevent;public class Keyutil {private Boolean Isvolumedown = false;private Boolean isvolumeup = False;priva Te boolean ismenu = false;private int currentkeycode = 0;private static Boolean Isdoubleclick = False;private static Boole an Islongclick = false; Checkforlongpress mpendingcheckforlongpress = null; Checkfordoublepress mpendingcheckfordoublepress = null; Handler Mhandler = new Handler (); Context Mcontext = null;private string tag = "";p ublic keyutil (context context, String tag) {mcontext = Context; tag = tag;} public void Dispatchkeyevent (KeyEvent event) {int keycode = Event.getkeycode ();//There are different keystrokes pressed, cancel long press, short press to determine if (Currentkeycode!). = KeyCode) {removelongpresscallback (); isdoubleclick = false;} Handle long press, click, double click Key if (event.getaction () = = Keyevent.action_down) {Checkforlongclick (event);} else if (event.getaction () = = KEYEVENT.ACTION_UP) {Checkfordoubleclick (event);} if (keycodE = = Keyevent.keycode_volume_down) {if (event.getaction () = = Keyevent.action_down) {Isvolumedown = true;} else if (EVENT.G Etaction () = = keyevent.action_up) {Isvolumedown = false;}} else if (keycode = = keyevent.keycode_volume_up) {if (event.getaction () = = Keyevent.action_down) {Isvolumeup = true;} else if (event.getaction () = = keyevent.action_up) {isvolumeup = false;}} else if (keycode = = Keyevent.keycode_menu) {if (event.getaction () = = Keyevent.action_down) {Ismenu = true;} else if (event . getaction () = = keyevent.action_up) {Ismenu = true;}} Determine the combination of keys if (isvolumedown&& isvolumeup&& ismenu&& (keycode = = keyevent.keycode_volume_up| | KeyCode = = Keyevent.keycode_volume_down | | KeyCode = = Keyevent.keycode_menu) && event.getaction () = = Keyevent.action_down) {//Combo key event handling; Isvolumedown = False;isvolumeup = False;ismenu = false;}} private void Removelongpresscallback () {if (mpendingcheckforlongpress! = null) {Mhandler.removecallbacks ( mpendingcheckforlongpress);}} Private void Checkforlongclick (KeyEvent event) {int count = Event.getrepeatcount (); int keycode = Event.getkeycode (); if (count = = 0) {Currentkeycode = KeyCode;} else {return;} if (mpendingcheckforlongpress = = null) {mpendingcheckforlongpress = new checkforlongpress ();} Mpendingcheckforlongpress.setkeycode (Event.getkeycode ()); mhandler.postdelayed (mpendingcheckforlongpress, 1000);} Class Checkforlongpress implements Runnable {int currentkeycode = 0;public void Run () {Islongclick = True;longpress (curren Tkeycode);} public void Setkeycode (int keycode) {currentkeycode = KeyCode;}} private void longpress (int keycode) {log.i (TAG, "--longpress long press Event--" + KeyCode);} private void Singleclick (int keycode) {log.i (TAG, "--singleclick Click Event--" + KeyCode);} private void doublepress (int keycode) {log.i (TAG, "---doublepress double-click event--" + KeyCode);} private void Checkfordoubleclick (KeyEvent event) {//has a long-on-time occurrence, does not process click, double-click event Removelongpresscallback (), if (Islongclick) { Islongclick = False;return;} if (!isdoubleclick) {Isdoubleclick = True;if (mpendingcheckfordoublepress = = null) {mpendingcheckfordoublepress = new checkfordoublepress ();} Mpendingcheckfordoublepress.setkeycode (Event.getkeycode ()); Mhandler.postdelayed (Mpendingcheckfordoublepress, 500);} else {//500ms within two clicks, triggering double-click Isdoubleclick = False;doublepress (Event.getkeycode ());}} Class Checkfordoublepress implements Runnable {int currentkeycode = 0;public void Run () {if (Isdoubleclick) {Singleclick (c Urrentkeycode);} Isdoubleclick = false;} public void Setkeycode (int keycode) {currentkeycode = KeyCode;}} private void Removedoublepresscallback () {if (mpendingcheckfordoublepress! = null) {Mhandler.removecallbacks ( mpendingcheckfordoublepress);}}}

Attention:
Only the action down state RepeatCount will be >0, avoid long press and click event Confusion;

Short-Press, double-click, long-press events for custom keystrokes in the Android system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.