Monodroid 中的 KeyPressed / KeyReleased 事件?

KeyPressed / KeyReleased events in Monodroid?

我正在尝试在按住按钮的同时连续发送内容。它是 Monodroid,所以我可以使用事件 OnTouchEvent,但这不是我要找的。我想要的只是一个像KeyPressed()这样的事件,所以它会运行连续的代码。

Monodroid 中也没有 KeyReleased() 事件,这意味着我需要使用其他东西来代替,也许 MotionEvent?我已经用 MotionEvent 尝试过一些东西,但效果不佳。

有没有人可以帮助我?

您可以利用 LongClick 事件:

var button = new Button(this);
button.LongClick += (object sender, Android.Views.View.LongClickEventArgs e) => {
};

按键事件:

button.KeyPress += (object sender, Android.Views.View.KeyEventArgs e) => {
};

参考:



编辑

利用OnTouch事件识别motionEvent。

例如:

public class MyTouchListener 
    : Java.Lang.Object
    , View.IOnTouchListener
{
    public bool OnTouch(View v, MotionEvent e)
    {
        if (e.Action == MotionEventActions.Down)
        {
            // do stuff
            return true;
        }
        if (e.Action == MotionEventActions.Up)
        {
            // do other stuff
            return true;
        }

        return false;
    }
}

    Button button = FindViewById<Button> (Resource.Id.myButton);
    var count = 0;
    var handled = false;
    button.Touch += (s, e) => {
        if(e.Event.Action == Android.Views.MotionEventActions.Down)
        {
            // Do stuff.
            System.Console.WriteLine("Counting ... " + count.ToString());
            count++;
            handled = true;
        }
        else if (e.Event.Action == Android.Views.MotionEventActions.Up)
        {
            // Do stuff.
            System.Console.WriteLine("Counting ... " + count.ToString());
            count++;
            handled = true;
        }
        e.Handled = handled;
    };

参考: