C# 上下左右键 切换控件焦点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 上下左右键 切换控件焦点相关的知识,希望对你有一定的参考价值。

form里面有textbox combobox listbox
寻求用上下左右键切换控件焦点的方法
C#
有没有通过坐标寻找控件的方法?

在键盘事件中捕捉上下左右键,注意设置窗体KeyPreview

添加一个2维Array,将画面控件按位置添加进去

通过上下左右键加减2维坐标

((Control)array[2,1]).SetFocus()

通过坐标寻找控件
Form.GetChildAtPoint(new Point(10, 10));
参考技术A 把下面这段代码写在窗体的KeyPress事件里,并且将窗体的KeyPreview属性置为True
private void Form1_KeyPress(object sender, KeyPressEventArgs e)

if (e.KeyChar == (char)(Keys.Down))

this.SelectNextControl(this.ActiveControl, true, false, false, true);


if (e.KeyChar == (char)(Keys.Up))

this.SelectNextControl(this.ActiveControl, false, false, false, true);


遇到textbox等输入控件,需要把对应的keypress事件指向窗口的keypress事件
参考技术B KeyDown事件里去判断

Android 系统APK-Camera 修复:预览照片时使用按键盘左右键,底部导航栏焦点也会跟着变动

改成了:默认底部导航栏禁用,但不消失,这样左右键切换时,只切换照片
若需要底部导航功能,按方向下键即可启用底部导航功能,此时左右键只切换底部导航按钮,若需要切换照片,则需要按上键,此时底部导航禁用,照片正常切换。从而达到修复的效果

packages/apps/Camera2/src/com/android/camera/CameraActivity.java

    //wangrui Bottom navigation bar enabled state Default false off, true on
    private boolean bottomPanelFlag = false;
    //wangrui get myFilmstripItem
    private FilmstripItem myFilmstripItem;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) 
        if (ActivityManager.isUserAMonkey())
            return super.onKeyUp(keyCode, event);
        if (!mFilmstripVisible) 
            // If a module is in the middle of capture, it should
            // consume the key event.
            if (mCurrentModule.onKeyUp(keyCode, event)) 
                Log.i(TAG, "onKeyUp->mCurrentModule->onKeyup->true");
                return true;
             else if (keyCode == KeyEvent.KEYCODE_MENU
                    || keyCode == KeyEvent.KEYCODE_DPAD_LEFT) 
                // Let the mode list view consume the event.
                mCameraAppUI.openModeList();
                return true;
             else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
                Log.i(TAG, "onKeyUp->keycode_dpad_down");
                if(mModeListView.getVisibility() == View.VISIBLE && mCurrentModeIndex == 0)
                    mModeListView.getListView().getChildAt(1).performClick();
                
             else if(keyCode == KeyEvent.KEYCODE_DPAD_UP)
                Log.i(TAG, "onKeyUp->keycode_dpad_up");
                if(mModeListView.getVisibility() == View.VISIBLE && mCurrentModeIndex == 1)
                    mModeListView.getListView().getChildAt(0).performClick();
                
             else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) 
                if (mDataAdapter.getTotalNumber() > 0)
                    mCameraAppUI.showFilmstrip();
                return true;
            
         else 

            // wangrui DOWN,Enable bottom navigation buttons
++            if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
++                bottomPanelFlag = true;
++                updateBottomControlsByData(myFilmstripItem);
++            else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) // UP Close bottom navigation buttons
++               bottomPanelFlag = false;
++                updateBottomControlsByData(myFilmstripItem);
++            

            // wangrui When the current state of the bottom navigation bar is true, switching is allowed, otherwise it is not allowed
            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) 
++                if (!bottomPanelFlag) mFilmstripController.goToNextItem();
                return true;
             else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) 
++                if (!bottomPanelFlag)
                    boolean wentToPrevious = mFilmstripController.goToPreviousItem();
                    if (!wentToPrevious) 
                        // at beginning of filmstrip, hide and go back to preview
                        mCameraAppUI.hideFilmstrip();
                    
++                
                return true;
            
        
        return super.onKeyUp(keyCode, event);
    


以上是关于C# 上下左右键 切换控件焦点的主要内容,如果未能解决你的问题,请参考以下文章

C#关于控件的上下左右移动

winform Form 内控件焦点变化?

vb 如何用上下左右键改变控件位置,但是不改变焦点位置?

qt焦点左右键切换问题,求大神指导!!

Qt控件焦点切换

WPF用键盘左右键按下获取按钮焦点,离开执行按钮事件