onKeyDown和onKeyLongPress

时间:2012-10-18 08:26:21

标签: android keylistener

我希望我的应用程序对音量按钮的正常和长按键事件做出不同的反应。

我已经看过this,但如果我按下音量按钮,我会在收到KeyDown事件之前收到很多KeyLongPressed个事件。

我想要一个或另一个,不是两个,所以我可以在短时间内调整音量并长按跳过音轨。

你能帮助我吗?

这是我的代码:

    @Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) 
    {
        Log.d("Test", "Long press!");
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        event.startTracking();
        Log.d("Test", "Short");
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

任何帮助表示赞赏!   - Iris

4 个答案:

答案 0 :(得分:15)

这是我写的代码。它就像一个魅力。也许你可以优化它以获得更好的逻辑。但你会明白这一点。关键是使用标志。 短按是我们短按时按音量键并释放的按键。所以 onKeyUp 是帮助我们检测短按的那个。

package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;

public class TestVolumeActivity extends Activity {
    boolean flag = false;

    boolean flag2 = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_splash_screen, menu);
        return true;
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            Log.d("Test", "Long press!");
            flag = false;
            flag2 = true;
            return true;
        }
        return super.onKeyLongPress(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            event.startTracking();
            if (flag2 == true) {
                flag = false;
            } else {
                flag = true;
                flag2 = false;
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {

            event.startTracking();
            if (flag) {
                Log.d("Test", "Short");
            }
            flag = true;
            flag2 = false;
            return true;
        }

        return super.onKeyUp(keyCode, event);
    }
}

所有长按的Logcat(未检测到短按):

10-18 02:06:15.369: D/Test(16834): Long press!
10-18 02:06:18.683: D/Test(16834): Long press!
10-18 02:06:21.566: D/Test(16834): Long press!
10-18 02:06:23.738: D/Test(16834): Long press!

所有短按的Logcat:

10-18 02:07:42.422: D/Test(16834): Short
10-18 02:07:43.203: D/Test(16834): Short
10-18 02:07:43.663: D/Test(16834): Short
10-18 02:07:44.144: D/Test(16834): Short

答案 1 :(得分:5)

根据SDK的正确方式来处理长按按钮。

import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;


public class TestVolumeActivity extends Activity
{
    private static final String TAG = TestVolumeActivity.class.getSimpleName();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if( keyCode == KeyEvent.KEYCODE_VOLUME_UP || 
            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
        {
            event.startTracking();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
            Log.d(TAG, "Long press KEYCODE_VOLUME_UP");
            return true;
        }
        else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
            Log.d(TAG, "Long press KEYCODE_VOLUME_DOWN");
            return true;
        }
        return super.onKeyLongPress(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        if((event.getFlags() & KeyEvent.FLAG_CANCELED_LONG_PRESS) == 0){
            if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
                Log.e(TAG, "Short press KEYCODE_VOLUME_UP");
                return true;
            }
            else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
                Log.e(TAG, "Short press KEYCODE_VOLUME_DOWN");
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }
}

答案 2 :(得分:4)

当我即将发布我的答案时,我发现有人已经得到某种解决方案......

但这是我的,简单,就像一个魅力。只有一面旗帜;)

此代码检测到短按和长按,当长按发生时,不会发出短按!

注意:如果你想要正常的音量增加和减少行为,请将onKeyPress方法中的返回true更改为超级调用,如下所示:

event.startTracking();
if(event.getRepeatCount() == 0){
    shortPress = true;
}
//return true;
return super.onKeyDown(keyCode, event);

没有超级电话的代码:

private boolean shortPress = false;

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        shortPress = false;
        Toast.makeText(this, "longPress", Toast.LENGTH_LONG).show();
        return true;
    }
    //Just return false because the super call does always the same (returning false)
    return false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            event.startTracking();
            if(event.getRepeatCount() == 0){
                shortPress = true;
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if(shortPress){
            Toast.makeText(this, "shortPress", Toast.LENGTH_LONG).show();
        } else {
            //Don't handle longpress here, because the user will have to get his finger back up first
        }
        shortPress = false;
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

这里的代码是添加了音量增加键,只需选择你的一面;)

private boolean shortPress = false;

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        shortPress = false;
        Toast.makeText(this, "longPress Volume Down", Toast.LENGTH_LONG).show();
        return true;
    } else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        shortPress = false;
        Toast.makeText(this, "longPress Volume Up", Toast.LENGTH_LONG).show();
        return true;
    }
    //Just return false because the super call does always the same (returning false)
    return false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            event.startTracking();
            if(event.getRepeatCount() == 0){
                shortPress = true;
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        if(shortPress){
            Toast.makeText(this, "shortPress Volume Down", Toast.LENGTH_LONG).show();
        } else {
            //Don't handle longpress here, because the user will have to get his finger back up first
        }
        shortPress = false;
        return true;
    } else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        if(shortPress){
            Toast.makeText(this, "shortPress Volume up", Toast.LENGTH_LONG).show();
        } else {
            //Don't handle longpress here, because the user will have to get his finger back up first
        }
        shortPress = false;
        return true;

    }
    return super.onKeyUp(keyCode, event);
}

答案 3 :(得分:0)

我不知道这个答案是否会为您的问题提供可接受的解决方案,因为它依赖于持续频繁 KeyDown事件。

您可以尝试记住上次KeyDown事件被抛出时的系统时间(我将其命名为tLast),并忽略所有KeyDown事件,直到获得{{1事件。

为了获得“正确的”KeyLongPressed事件(您在上一步中忽略的事件),您可以让线程检查当前系统时间与KeyDown之间的时差(我将它命名为tLast)足够大,以免被视为连续按压。

鉴于在很短的时间内抛出了很多tDelta个事件,理论上可以确定当事件间隔足够时,音量按钮没有连续按下(KeyDown更大比固定值。)

该解决方案的缺点是,如果用户非常快地按下音量按钮(按下tDelta低于用于评估连续按下的固定值),则按下多个按键将是被忽视/被视为连续按键。

另一个(次要的)缺点是在解释常规按键之前会有延迟,因为tDelta必须大于评估你是否正在处理常规或连续按键。

此致

卢西恩

编辑: 嗯......第二个想法:你不是在使用tDelta Android实现吗?

如果您使用此功能,请查看为此定义的KeyListener方法。

我认为如果您只是扩展此类(或其中一个派生类)并使用onKeyUp方法来确定您是否正在处理连续按下(即时间按钮已被按下大于固定值)。

如果您可以使用此方法,请执行此操作。它比上面提到的解决方案更有效,更易于维护和更直接。

KeyListener Javadoc