[dali_1.2.29] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "keyboard-focus-manager-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/devel-api/adaptor-framework/accessibility-adaptor.h>
25 #include <dali/devel-api/adaptor-framework/singleton-service.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/events/key-event.h>
29 #include <dali/public-api/events/touch-data.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/public-api/object/type-registry-helper.h>
32 #include <dali/public-api/images/resource-image.h>
33 #include <dali/integration-api/debug.h>
34
35 // INTERNAL INCLUDES
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/controls/control-impl.h>
38 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
39 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
40 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
41 #include <dali-toolkit/devel-api/controls/control-devel.h>
42
43 namespace Dali
44 {
45
46 namespace Toolkit
47 {
48
49 namespace Internal
50 {
51
52 namespace // Unnamed namespace
53 {
54
55 #if defined(DEBUG_ENABLED)
56 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
57 #endif
58
59 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
60
61 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.9.png";
62
63 BaseHandle Create()
64 {
65   BaseHandle handle = KeyboardFocusManager::Get();
66
67   if ( !handle )
68   {
69     SingletonService singletonService( SingletonService::Get() );
70     if ( singletonService )
71     {
72       Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
73       singletonService.Register( typeid( manager ), manager );
74       handle = manager;
75     }
76   }
77
78   return handle;
79 }
80
81 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
82
83 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange",           SIGNAL_PRE_FOCUS_CHANGE )
84 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged",             SIGNAL_FOCUS_CHANGED )
85 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged",        SIGNAL_FOCUS_GROUP_CHANGED )
86 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey",     SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
87
88 DALI_TYPE_REGISTRATION_END()
89
90 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
91
92 } // unnamed namespace
93
94 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
95 {
96   Toolkit::KeyboardFocusManager manager;
97
98   SingletonService singletonService( SingletonService::Get() );
99   if ( singletonService )
100   {
101     // Check whether the keyboard focus manager is already created
102     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
103     if(handle)
104     {
105       // If so, downcast the handle of singleton to keyboard focus manager
106       manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
107     }
108   }
109
110   return manager;
111 }
112
113 KeyboardFocusManager::KeyboardFocusManager()
114 : mPreFocusChangeSignal(),
115   mFocusChangedSignal(),
116   mFocusGroupChangedSignal(),
117   mFocusedActorEnterKeySignal(),
118   mCurrentFocusActor( 0 ),
119   mFocusIndicatorActor(),
120   mFocusGroupLoopEnabled( false ),
121   mIsFocusIndicatorEnabled( false ),
122   mIsWaitingKeyboardFocusChangeCommit( false ),
123   mFocusHistory(),
124   mSlotDelegate( this )
125 {
126   // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorEnabled.
127   Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
128   Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
129 }
130
131 KeyboardFocusManager::~KeyboardFocusManager()
132 {
133 }
134
135 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
136 {
137   DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
138
139   if( actor )
140   {
141     return DoSetCurrentFocusActor( actor.GetId() );
142   }
143
144   return false;
145 }
146
147 bool KeyboardFocusManager::DoSetCurrentFocusActor( const unsigned int actorID )
148 {
149   Actor rootActor = Stage::GetCurrent().GetRootLayer();
150   Actor actor = rootActor.FindChildById( actorID );
151   bool success = false;
152
153   // Check whether the actor is in the stage and is keyboard focusable.
154   if( actor && actor.IsKeyboardFocusable() )
155   {
156     if( mIsFocusIndicatorEnabled )
157     {
158       actor.Add( GetFocusIndicatorActor() );
159     }
160     // Send notification for the change of focus actor
161     Actor currentFocusedActor = GetCurrentFocusActor();
162
163     if( !mFocusChangedSignal.Empty() )
164     {
165       mFocusChangedSignal.Emit(currentFocusedActor, actor);
166     }
167
168     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
169     if( currentlyFocusedControl )
170     {
171       // Do we need it to remember if it was previously DISABLED?
172       currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
173     }
174
175     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
176
177     // Save the current focused actor
178     mCurrentFocusActor = actorID;
179
180     Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
181     if( newlyFocusedControl )
182     {
183       newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
184     }
185
186     // Push Current Focused Actor to FocusHistory
187     mFocusHistory.PushBack( &actor.GetBaseObject() );
188
189     // Delete first element before add new element when Stack is full.
190     if( mFocusHistory.Count() > MAX_HISTORY_AMOUNT )
191     {
192        FocusStackIterator beginPos = mFocusHistory.Begin();
193        mFocusHistory.Erase( beginPos );
194     }
195
196     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
197     success = true;
198   }
199   else
200   {
201     DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
202   }
203
204   return success;
205 }
206
207 Actor KeyboardFocusManager::GetCurrentFocusActor()
208 {
209   Actor rootActor = Stage::GetCurrent().GetRootLayer();
210   return rootActor.FindChildById(mCurrentFocusActor);
211 }
212
213 Actor KeyboardFocusManager::GetCurrentFocusGroup()
214 {
215   return GetFocusGroup(GetCurrentFocusActor());
216 }
217
218 void KeyboardFocusManager::MoveFocusBackward()
219 {
220   // Find Pre Focused Actor when the list size is more than 1
221   if( mFocusHistory.Count() > 1 )
222   {
223     // Delete current focused actor in history
224     FocusStackIterator endPos = mFocusHistory.End();
225     endPos = mFocusHistory.Erase( --endPos );
226
227     // If pre-focused actors are not on stage, remove them in stack
228     while( !Dali::Actor::DownCast(BaseHandle(mFocusHistory[ mFocusHistory.Count() - 1 ])).OnStage() )
229     {
230       endPos = mFocusHistory.Erase( --endPos );
231     }
232
233     // Get pre focused actor
234     BaseObject* object = mFocusHistory[ mFocusHistory.Count() - 1 ];
235     BaseHandle handle( object );
236     Actor preFocusedActor = Dali::Actor::DownCast( handle );
237
238     // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
239     mFocusHistory.Erase( --endPos );
240
241     SetCurrentFocusActor( preFocusedActor );
242  }
243 }
244
245 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
246 {
247   Toolkit::Control control = Toolkit::Control::DownCast(actor);
248   return control && GetImplementation( control ).IsKeyboardNavigationSupported();
249 }
250
251 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
252 {
253   // Get the actor's parent layout control that supports two dimensional keyboard navigation
254   Actor rootActor = Stage::GetCurrent().GetRootLayer();
255   Actor parent;
256   if(actor)
257   {
258     parent = actor.GetParent();
259   }
260
261   while( parent && !IsLayoutControl(parent) && parent != rootActor )
262   {
263     parent = parent.GetParent();
264   }
265
266   return Toolkit::Control::DownCast(parent);
267 }
268
269 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
270 {
271   Actor currentFocusActor = GetCurrentFocusActor();
272
273   bool succeed = false;
274
275   // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
276   Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
277   while(parentLayoutControl && !succeed)
278   {
279     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
280     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
281   }
282
283   if(!succeed && !mPreFocusChangeSignal.Empty())
284   {
285     // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
286     mIsWaitingKeyboardFocusChangeCommit = true;
287     Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
288     mIsWaitingKeyboardFocusChangeCommit = false;
289
290     if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
291     {
292       // Whether the next focusable actor is a layout control
293       if(IsLayoutControl(nextFocusableActor))
294       {
295         // If so, move the focus inside it.
296         Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
297         succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
298       }
299       else
300       {
301         // Otherwise, just set focus to the next focusable actor
302         succeed = SetCurrentFocusActor(nextFocusableActor);
303       }
304     }
305   }
306
307   return succeed;
308 }
309
310 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
311 {
312   // Ask the control for the next actor to focus
313   Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
314   if(nextFocusableActor)
315   {
316     if(!nextFocusableActor.IsKeyboardFocusable())
317     {
318       // If the actor is not focusable, ask the same layout control for the next actor to focus
319       return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
320     }
321     else
322     {
323       Actor currentFocusActor = GetCurrentFocusActor();
324       Actor committedFocusActor = nextFocusableActor;
325
326       // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
327       // Signal handler can check the proposed actor and return a different actor if it wishes.
328       if( !mPreFocusChangeSignal.Empty() )
329       {
330         mIsWaitingKeyboardFocusChangeCommit = true;
331         committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
332         mIsWaitingKeyboardFocusChangeCommit = false;
333       }
334
335       if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
336       {
337         // Whether the commited focusable actor is a layout control
338         if(IsLayoutControl(committedFocusActor))
339         {
340           // If so, move the focus inside it.
341           Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
342           return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
343         }
344         else
345         {
346           // Otherwise, just set focus to the next focusable actor
347           if(committedFocusActor == nextFocusableActor)
348           {
349             // If the application hasn't changed our proposed actor, we informs the layout control we will
350             // move the focus to what the control returns. The control might wish to perform some actions
351             // before the focus is actually moved.
352             GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
353           }
354
355           return SetCurrentFocusActor(committedFocusActor);
356         }
357       }
358       else
359       {
360         return false;
361       }
362     }
363   }
364   else
365   {
366     // No more actor can be focused in the given direction within the same layout control.
367     return false;
368   }
369 }
370
371 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
372 {
373   bool succeed = false;
374
375   // Get the parent layout control of the current focus group
376   Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
377
378   while(parentLayoutControl && !succeed)
379   {
380     // If the current focus group has a parent layout control, we can probably automatically
381     // move the focus to the next focus group in the forward or backward direction.
382     Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
383     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
384     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
385   }
386
387   if(!mFocusGroupChangedSignal.Empty())
388   {
389     // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
390     mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
391   }
392
393   return succeed;
394 }
395
396 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
397 {
398   if( actor )
399   {
400     Toolkit::Control control = Toolkit::Control::DownCast( actor );
401     if( control )
402     {
403       // Notify the control that enter has been pressed on it.
404       GetImplementation( control ).KeyboardEnter();
405     }
406
407     // Send a notification for the actor.
408     if( !mFocusedActorEnterKeySignal.Empty() )
409     {
410       mFocusedActorEnterKeySignal.Emit( actor );
411     }
412   }
413 }
414
415 void KeyboardFocusManager::ClearFocus()
416 {
417   Actor actor = GetCurrentFocusActor();
418   if( actor )
419   {
420     if( mFocusIndicatorActor )
421     {
422       actor.Remove( mFocusIndicatorActor );
423     }
424
425     // Send notification for the change of focus actor
426     if( !mFocusChangedSignal.Empty() )
427     {
428       mFocusChangedSignal.Emit( actor, Actor() );
429     }
430
431     Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
432     if( currentlyFocusedControl )
433     {
434       currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
435     }
436   }
437
438   mCurrentFocusActor = 0;
439   mIsFocusIndicatorEnabled = false;
440 }
441
442 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
443 {
444   mFocusGroupLoopEnabled = enabled;
445 }
446
447 bool KeyboardFocusManager::GetFocusGroupLoop() const
448 {
449   return mFocusGroupLoopEnabled;
450 }
451
452 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
453 {
454   if(actor)
455   {
456     // Create/Set focus group property.
457     actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
458   }
459 }
460
461 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
462 {
463   // Check whether the actor is a focus group
464   bool isFocusGroup = false;
465
466   if(actor)
467   {
468     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
469     if(propertyIsFocusGroup != Property::INVALID_INDEX)
470     {
471       isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
472     }
473   }
474
475   return isFocusGroup;
476 }
477
478 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
479 {
480   // Go through the actor's hierarchy to check which focus group the actor belongs to
481   while (actor && !IsFocusGroup(actor))
482   {
483     actor = actor.GetParent();
484   }
485
486   return actor;
487 }
488
489 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
490 {
491   if(mFocusIndicatorActor != indicator)
492   {
493     Actor currentFocusActor = GetCurrentFocusActor();
494     if(currentFocusActor)
495     {
496       // The new focus indicator should be added to the current focused actor immediately
497       if(mFocusIndicatorActor)
498       {
499         currentFocusActor.Remove(mFocusIndicatorActor);
500       }
501
502       if(indicator)
503       {
504         currentFocusActor.Add(indicator);
505       }
506     }
507
508     mFocusIndicatorActor = indicator;
509   }
510 }
511
512 Actor KeyboardFocusManager::GetFocusIndicatorActor()
513 {
514   if( ! mFocusIndicatorActor )
515   {
516     // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
517     mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
518     mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
519
520     // Apply size constraint to the focus indicator
521     mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
522   }
523
524   return mFocusIndicatorActor;
525 }
526
527 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
528 {
529   AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
530   bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
531
532   Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
533
534   std::string keyName = event.keyPressedName;
535
536   bool isFocusStartableKey = false;
537
538   if(event.state == KeyEvent::Down)
539   {
540     if (keyName == "Left")
541     {
542       if(!isAccessibilityEnabled)
543       {
544         if(!mIsFocusIndicatorEnabled)
545         {
546           // Show focus indicator
547           mIsFocusIndicatorEnabled = true;
548         }
549         else
550         {
551           // Move the focus towards left
552           MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
553         }
554
555         isFocusStartableKey = true;
556       }
557       else
558       {
559         // Move the accessibility focus backward
560         accessibilityManager.MoveFocusBackward();
561       }
562     }
563     else if (keyName == "Right")
564     {
565       if(!isAccessibilityEnabled)
566       {
567         if(!mIsFocusIndicatorEnabled)
568         {
569           // Show focus indicator
570           mIsFocusIndicatorEnabled = true;
571         }
572         else
573         {
574           // Move the focus towards right
575           MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
576         }
577       }
578       else
579       {
580         // Move the accessibility focus forward
581         accessibilityManager.MoveFocusForward();
582       }
583
584       isFocusStartableKey = true;
585     }
586     else if (keyName == "Up" && !isAccessibilityEnabled)
587     {
588       if(!mIsFocusIndicatorEnabled)
589       {
590         // Show focus indicator
591         mIsFocusIndicatorEnabled = true;
592       }
593       else
594       {
595         // Move the focus towards up
596         MoveFocus(Toolkit::Control::KeyboardFocus::UP);
597       }
598
599       isFocusStartableKey = true;
600     }
601     else if (keyName == "Down" && !isAccessibilityEnabled)
602     {
603       if(!mIsFocusIndicatorEnabled)
604       {
605         // Show focus indicator
606         mIsFocusIndicatorEnabled = true;
607       }
608       else
609       {
610         // Move the focus towards down
611         MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
612       }
613
614       isFocusStartableKey = true;
615     }
616     else if (keyName == "Prior" && !isAccessibilityEnabled)
617     {
618       if(!mIsFocusIndicatorEnabled)
619       {
620         // Show focus indicator
621         mIsFocusIndicatorEnabled = true;
622       }
623       else
624       {
625         // Move the focus towards the previous page
626         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
627       }
628
629       isFocusStartableKey = true;
630     }
631     else if (keyName == "Next" && !isAccessibilityEnabled)
632     {
633       if(!mIsFocusIndicatorEnabled)
634       {
635         // Show focus indicator
636         mIsFocusIndicatorEnabled = true;
637       }
638       else
639       {
640         // Move the focus towards the next page
641         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
642       }
643
644       isFocusStartableKey = true;
645     }
646     else if (keyName == "Tab" && !isAccessibilityEnabled)
647     {
648       if(!mIsFocusIndicatorEnabled)
649       {
650         // Show focus indicator
651         mIsFocusIndicatorEnabled = true;
652       }
653       else
654       {
655         // "Tab" key changes the focus group in the forward direction and
656         // "Shift-Tab" key changes it in the backward direction.
657         DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
658       }
659
660       isFocusStartableKey = true;
661     }
662     else if (keyName == "space" && !isAccessibilityEnabled)
663     {
664       if(!mIsFocusIndicatorEnabled)
665       {
666         // Show focus indicator
667         mIsFocusIndicatorEnabled = true;
668       }
669
670       isFocusStartableKey = true;
671     }
672     else if (keyName == "" && !isAccessibilityEnabled)
673     {
674       // Check the fake key event for evas-plugin case
675       if(!mIsFocusIndicatorEnabled)
676       {
677         // Show focus indicator
678         mIsFocusIndicatorEnabled = true;
679       }
680
681       isFocusStartableKey = true;
682     }
683     else if (keyName == "Backspace" && !isAccessibilityEnabled)
684     {
685       // Emit signal to go back to the previous view???
686     }
687     else if (keyName == "Escape" && !isAccessibilityEnabled)
688     {
689     }
690   }
691   else if(event.state == KeyEvent::Up)
692   {
693     if (keyName == "Return")
694     {
695       if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
696       {
697         // Show focus indicator
698         mIsFocusIndicatorEnabled = true;
699       }
700       else
701       {
702         // The focused actor has enter pressed on it
703         Actor actor;
704         if( !isAccessibilityEnabled )
705         {
706           actor = GetCurrentFocusActor();
707         }
708         else
709         {
710           actor = accessibilityManager.GetCurrentFocusActor();
711         }
712
713         if( actor )
714         {
715           DoKeyboardEnter( actor );
716         }
717       }
718
719       isFocusStartableKey = true;
720     }
721   }
722
723   if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
724   {
725     Actor actor = GetCurrentFocusActor();
726     if( actor )
727     {
728       // Make sure the focused actor is highlighted
729       actor.Add( GetFocusIndicatorActor() );
730     }
731     else
732     {
733       // No actor is focused but keyboard focus is activated by the key press
734       // Let's try to move the initial focus
735       MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
736     }
737   }
738 }
739
740 void KeyboardFocusManager::OnTouch(const TouchData& touch)
741 {
742   // Clear the focus when user touch the screen.
743   // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
744   if( ( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN ) )
745   {
746     ClearFocus();
747   }
748 }
749
750 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
751 {
752   return mPreFocusChangeSignal;
753 }
754
755 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
756 {
757   return mFocusChangedSignal;
758 }
759
760 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
761 {
762   return mFocusGroupChangedSignal;
763 }
764
765 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
766 {
767   return mFocusedActorEnterKeySignal;
768 }
769
770 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
771 {
772   Dali::BaseHandle handle( object );
773
774   bool connected( true );
775   KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
776
777   if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
778   {
779     manager->PreFocusChangeSignal().Connect( tracker, functor );
780   }
781   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
782   {
783     manager->FocusChangedSignal().Connect( tracker, functor );
784   }
785   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
786   {
787     manager->FocusGroupChangedSignal().Connect( tracker, functor );
788   }
789   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
790   {
791     manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
792   }
793   else
794   {
795     // signalName does not match any signal
796     connected = false;
797   }
798
799   return connected;
800 }
801
802 } // namespace Internal
803
804 } // namespace Toolkit
805
806 } // namespace Dali