@@ -626,45 +626,46 @@ public static void onKeyEvent(int down, long keysym, long client) {
626626 Get current keyboard focus node for input context's display.
627627 */
628628 AccessibilityNodeInfo currentFocusNode = instance .mKeyboardFocusNodes .get (inputContext .getDisplayId ());
629-
630- if (currentFocusNode == null ) {
631- Log .w (TAG , "onKeyEvent: no focus node for display " + inputContext .getDisplayId () + ", trying to find one" );
632- if (Build .VERSION .SDK_INT >= 30 ) {
633- for (AccessibilityWindowInfo window : instance .getWindows ()) {
634- if (window .getDisplayId () == inputContext .getDisplayId ()) {
635- AccessibilityNodeInfo focusableNode = findFocusableNode (window .getRoot ());
636- if (focusableNode != null ) {
637- focusableNode .performAction (AccessibilityNodeInfo .ACTION_FOCUS );
638- currentFocusNode = focusableNode ;
639- Log .i (TAG , "onKeyEvent: Found and focused a new node." );
640- break ;
641- }
642- }
643- }
644- } else {
645- // Fallback for older APIs
646- AccessibilityNodeInfo rootNode = instance .getRootInActiveWindow ();
647- AccessibilityNodeInfo focusableNode = findFocusableNode (rootNode );
648- if (focusableNode != null ) {
649- focusableNode .performAction (AccessibilityNodeInfo .ACTION_FOCUS );
650- currentFocusNode = focusableNode ;
651- Log .i (TAG , "onKeyEvent: Found and focused a new node in the active window." );
652- }
653- }
654- }
655-
656- if (currentFocusNode == null ) {
657- Log .e (TAG , "onKeyEvent: Could not find any focusable node on display " + inputContext .getDisplayId () + ". Ignoring key event." );
658- return ;
629+ if (currentFocusNode != null ) {
630+ currentFocusNode .refresh ();
659631 }
660- // refresh() is important to load the represented view's current text into the node
661- currentFocusNode .refresh ();
662632
663633 /*
664634 DPAD Left/Right/Up/Down
665635 */
666636 if ((keysym == 0xff51 || keysym == 0xff52 || keysym == 0xff53 || keysym == 0xff54 ) && down != 0 ) {
667637
638+ if (currentFocusNode == null ) {
639+ Log .w (TAG , "onKeyEvent: no focus node for display " + inputContext .getDisplayId () + ", trying to find one" );
640+ if (Build .VERSION .SDK_INT >= 30 ) {
641+ for (AccessibilityWindowInfo window : instance .getWindows ()) {
642+ if (window .getDisplayId () == inputContext .getDisplayId ()) {
643+ AccessibilityNodeInfo focusableNode = findFocusableNode (window .getRoot ());
644+ if (focusableNode != null ) {
645+ focusableNode .performAction (AccessibilityNodeInfo .ACTION_FOCUS );
646+ currentFocusNode = focusableNode ;
647+ Log .i (TAG , "onKeyEvent: Found and focused a new node." );
648+ break ;
649+ }
650+ }
651+ }
652+ } else {
653+ // Fallback for older APIs
654+ AccessibilityNodeInfo rootNode = instance .getRootInActiveWindow ();
655+ AccessibilityNodeInfo focusableNode = findFocusableNode (rootNode );
656+ if (focusableNode != null ) {
657+ focusableNode .performAction (AccessibilityNodeInfo .ACTION_FOCUS );
658+ currentFocusNode = focusableNode ;
659+ Log .i (TAG , "onKeyEvent: Found and focused a new node in the active window." );
660+ }
661+ }
662+ }
663+
664+ if (currentFocusNode == null ) {
665+ Log .e (TAG , "onKeyEvent: Could not find any focusable node on display " + inputContext .getDisplayId () + ". Ignoring key event." );
666+ return ;
667+ }
668+
668669 boolean supportsTextTraversal = false ;
669670 for (AccessibilityNodeInfo .AccessibilityAction a : currentFocusNode .getActionList ()) {
670671 String className = currentFocusNode .getClassName ().toString ();
0 commit comments