Is there a way to prevent the KeyUp event?

Asked 2 years ago, Updated 2 years ago, 149 views

    public override Boolean DispatchKeyEvent(KeyEvent e)
    {
        _Main_Fragment = FragmentManager.BeginTransaction();
        Menu.Main_Title_Fragment _Main_Title_Fragment = new Menu.Main_Title_Fragment();

        switch(e.KeyCode)
        {
            case Keycode.Num0:

                if(e.Action == KeyEventActions.Up && e.IsLongPress == false)
                {
                    //Console.WriteLine("+++++++++++++++++++++++++++++++ KeyDeviceId : " + e.DeviceId + ", KeyCode : " + e.KeyCode + ", KeyScanCode : " + e.ScanCode);

                    LayoutInflater _Adjust_inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
                    View _Adjust_Popup = _Adjust_inflater.Inflate(Resource.Layout.Menu_Adjust_Window, null);

                    PopupWindow _Adjust_Window = new PopupWindow(_Adjust_Popup, 700, 300);
                    _Adjust_Window.SetBackgroundDrawable(new BitmapDrawable());
                    _Adjust_Window.OutsideTouchable = true;

                    _Adjust_Window.Focusable = true;
                    _Adjust_Window.ShowAtLocation(_Adjust_Popup, GravityFlags.Center, 0, 0);

                    _Gain_Switch = (Switch)_Adjust_Popup.FindViewById(Resource.Id._Gain_Switch);
                    _Gain_SeekBar = (SeekBar)_Adjust_Popup.FindViewById(Resource.Id._Gain_SeekBar);
                    _Gain_SeekBar_Text = (TextView)_Adjust_Popup.FindViewById(Resource.Id._Gain_SeekBar_Text);

                    _Gain_Switch.Checked = false;
                    _Gain_SeekBar.Enabled = false;
                    _Gain_SeekBar_Text.Text = "0";
                    _Gain_SeekBar_Text.SetTextColor(Color.Gray);

                    _Gain_Switch.CheckedChange += _Gain_Switch_CheckedChange;


                    _Sea_Switch = (Switch)_Adjust_Popup.FindViewById(Resource.Id._Sea_Switch);
                    _Sea_SeekBar = (SeekBar)_Adjust_Popup.FindViewById(Resource.Id._Sea_SeekBar);
                    _Sea_SeekBar_Text = (TextView)_Adjust_Popup.FindViewById(Resource.Id._Sea_SeekBar_Text);

                    _Sea_Switch.Checked = false;
                    _Sea_SeekBar.Enabled = false;
                    _Sea_SeekBar_Text.Text = "0";
                    _Sea_SeekBar_Text.SetTextColor(Color.Gray);

                    _Sea_Switch.CheckedChange += _Sea_Switch_CheckedChange;


                    _Rain_Switch = (Switch)_Adjust_Popup.FindViewById(Resource.Id._Rain_Switch);
                    _Rain_SeekBar = (SeekBar)_Adjust_Popup.FindViewById(Resource.Id._Rain_SeekBar);
                    _Rain_SeekBar_Text = (TextView)_Adjust_Popup.FindViewById(Resource.Id._Rain_SeekBar_Text);

                    _Rain_Switch.Checked = false;
                    _Rain_SeekBar.Enabled = false;
                    _Rain_SeekBar_Text.Text = "0";
                    _Rain_SeekBar_Text.SetTextColor(Color.Gray);

                    _Rain_Switch.CheckedChange += _Rain_Switch_CheckedChange;
                }

                if (e.IsLongPress == true)
                {
                    LayoutInflater _Power_infalter = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
                    View _Power_Popup = _Power_infalter.Inflate(Resource.Layout.Menu_Power_Window, null);

                    PopupWindow _Power = new PopupWindow(_Power_Popup, 500, 300);
                    _Power.SetBackgroundDrawable(new BitmapDrawable());

                    ImageButton _Custom_Back_Button = (ImageButton)_Power_Popup.FindViewById(Resource.Id._Custom_Back_Button);
                    ImageButton _Custom_Exit_Button = (ImageButton)_Power_Popup.FindViewById(Resource.Id._Custom_Exit_Button);

                    _Power.ShowAtLocation(_Power_Popup, GravityFlags.Center, 0, 0);

                    _Custom_Back_Button.Click += (a, b) =>
                    {
                        _Power.Dismiss();
                    };

                    _Custom_Exit_Button.Click += (u, n) =>
                    {
                        Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
                    };

                }
                return true;

        }

        return base.DispatchKeyEvent(e);
    }

This is the current code, but the KeyUp part is a temporary pop-up window. If you run it and press the hard key, If you press it for a short time, of course the event in KeyUp will occur When pressed for a long time with LongPress, the second event occurs, and of course, the KeyUp event occurs as soon as the button is touched.

But what I want to do is The event on KeyUp occurs only when you press and touch the button I want to prevent KeyUp from occurring when an event occurs in LongPress.

What should I do?

key hardkey

2022-09-21 16:17

1 Answers

https://developer.android.com/reference/android/view/KeyEvent

I think you should check and block flags related to key events such as isLongPress().

Basically, the person who controls the occurrence of the key event can hook up about the key event inside the Android Framework, but the app terminal receives and processes the event sent by the Framework, so the desired key action should be prevented by the application.


2022-09-21 16:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.