KeyUp 이벤트를 막는 방법이 있나요?

조회수 1046회
    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);
    }

현재 코드인데, KeyUp 부분에는 임시로 넣은 팝업창입니다. 이 상태에서 실행시켜서 하드키를 누르면 짧게 눌렀다 때면 당연히 KeyUp에 있는 이벤트가 발생합니다만, LongPress로 길게 눌렀을 때, 두번째 이벤트가 발생하면 버튼에서 손을 때는 순간 당연히 같이 KeyUp 이벤트도 발생합니다.

근데 제가 하고 싶은 건 KeyUp 에 이벤트는 진짜 딱 버튼을 한번 눌렀다땟을때만 발생하고, LongPress에 이벤트가 발생했을 때는 KeyUp이 발생안하게 하고 싶습니다..

어떻게 해야할까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

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

    에서 isLongPress() 등 키 이벤트 관련 flag 를 확인해서 막으셔야 할거 같습니다.

    기본적으로 keyEvent 발생을 제어하는 쪽은 Android Framework 내부에서 KeyEvent 관련 후킹을 할 수 있으나 App 단에서는 Framework 에서 보내주는 이벤트를 받아서 처리 하기 때문에 원하는 키동작은 Application 에서 막아야 할거 같습니다.

    • 그럼 시스템쪽에서 막아줘야한다는 건가요? 알 수 없는 사용자 2018.9.6 11:29
    • 네 하지만,, 제조사에서만 가능하겠죠. 프레임워크내부를 수정 하면 구글의 GTS 테스트를 통과해야되서 비용도 크고 합니다. 난이도도 높구요. APP 에서 노가다로 막으시는게 정신건강에 이롭습니다. 임재훈 2018.9.6 12:05
    • 음 APP에서 막는다는게 어떤 방식을 말씀하시는거에요?? 알 수 없는 사용자 2018.9.6 17:15
    • ㅠㅠ 아이구 이미다 테스트 해보셨었네요. 도움이 안됬네요 ㅠㅠ 임재훈 2018.9.7 08:14
    • 괜찮습니다 ㅎ 감사합니다~! 알 수 없는 사용자 2018.9.10 09:40

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)