From af012cfa29f839f6adc2278265247cb04bc0c7b5 Mon Sep 17 00:00:00 2001 From: Adeel Kazmi Date: Wed, 18 Sep 2019 14:44:06 +0100 Subject: [PATCH] Added handling for Back/Escape key to exit all demos (#4) Change-Id: I6a69501f9d5e74f7e5024da9b4752bb4e653972e --- food-shopper/FoodShopper.cs | 19 +++++++++- hello-world/HelloWorld.cs | 23 ++++++++++++ layout-demo/LayoutDemo.cs | 23 ++++++++++++ multiple-window/MultipleWindow.cs | 23 +++++++++++- silk-demo/SilkDemo.cs | 8 ++++- simple-layout/SimpleLayout.cs | 23 +++++++++++- simple-text/simple-text.cs | 59 ++++++++++++++++++------------- 7 files changed, 150 insertions(+), 28 deletions(-) mode change 100755 => 100644 simple-text/simple-text.cs diff --git a/food-shopper/FoodShopper.cs b/food-shopper/FoodShopper.cs index c0a01d7..de620ca 100644 --- a/food-shopper/FoodShopper.cs +++ b/food-shopper/FoodShopper.cs @@ -203,17 +203,34 @@ private void WindowKeyEvent(object sender, Window.KeyEventArgs e) switch( e.Key.KeyPressedName ) { case "Left": + { HorizontalOperation( false ); - + } break; + case "Right" : + { HorizontalOperation( true ); + } break; + case "Up" : + { FocusSectionUp(); + } break; + case "Down" : + { FocusSectionDown(); + } + break; + + case "Escape": + case "Back": + { + Exit(); + } break; } diff --git a/hello-world/HelloWorld.cs b/hello-world/HelloWorld.cs index 0c15c6a..85ced89 100644 --- a/hello-world/HelloWorld.cs +++ b/hello-world/HelloWorld.cs @@ -49,6 +49,29 @@ protected override void OnCreate() // Add the text to the window window.Add(title); + + // Respond to key events + window.KeyEvent += OnKeyEvent; + } + + /// + /// Called when any key event is received. + /// Will use this to exit the application if the Back or Escape key is pressed + /// + private void OnKeyEvent( object sender, Window.KeyEventArgs eventArgs ) + { + if( eventArgs.Key.State == Key.StateType.Down ) + { + switch( eventArgs.Key.KeyPressedName ) + { + case "Escape": + case "Back": + { + Exit(); + } + break; + } + } } /// diff --git a/layout-demo/LayoutDemo.cs b/layout-demo/LayoutDemo.cs index 0cbcd2a..5fbb795 100644 --- a/layout-demo/LayoutDemo.cs +++ b/layout-demo/LayoutDemo.cs @@ -161,6 +161,29 @@ private void Initialize() // Set initial title exampleTitle.Text = currentExampleLabel; + + // Respond to key events + Window.Instance.KeyEvent += OnKeyEvent; + } + + /// + /// Called when any key event is received. + /// Will use this to exit the application if the Back or Escape key is pressed + /// + private void OnKeyEvent( object sender, Window.KeyEventArgs eventArgs ) + { + if( eventArgs.Key.State == Key.StateType.Down ) + { + switch( eventArgs.Key.KeyPressedName ) + { + case "Escape": + case "Back": + { + Exit(); + } + break; + } + } } /// diff --git a/multiple-window/MultipleWindow.cs b/multiple-window/MultipleWindow.cs index 0283756..782c06c 100644 --- a/multiple-window/MultipleWindow.cs +++ b/multiple-window/MultipleWindow.cs @@ -83,9 +83,10 @@ protected override void OnCreate() // Up call to the Base class first base.OnCreate(); - // Get the main window instance and change background color + // Get the main window instance, change background color & respond to key events Window mainWindow = Window.Instance; mainWindow.BackgroundColor = Color.Red; + mainWindow.KeyEvent += OnKeyEvent; var layout = new LinearLayout() { @@ -141,6 +142,26 @@ protected override void OnCreate() _focusManager.FocusIndicator = new View(); } + /// + /// Called when any key event is received. + /// Will use this to exit the application if the Back or Escape key is pressed + /// + private void OnKeyEvent( object sender, Window.KeyEventArgs eventArgs ) + { + if( eventArgs.Key.State == Key.StateType.Down ) + { + switch( eventArgs.Key.KeyPressedName ) + { + case "Escape": + case "Back": + { + Exit(); + } + break; + } + } + } + private View OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e) { if (e.CurrentView != null && !e.ProposedView) diff --git a/silk-demo/SilkDemo.cs b/silk-demo/SilkDemo.cs index e2fb19e..5aec609 100644 --- a/silk-demo/SilkDemo.cs +++ b/silk-demo/SilkDemo.cs @@ -533,6 +533,12 @@ private void WindowKeyEvent(object sender, Window.KeyEventArgs e) EventAction(Action.ShowList); } break; + case "Escape" : + case "Back" : + { + Exit(); + } + break; } } } @@ -600,4 +606,4 @@ static void Main(string[] args) silkDemo.Run(args); } } // Class Demo -} // namespace Silk \ No newline at end of file +} // namespace Silk diff --git a/simple-layout/SimpleLayout.cs b/simple-layout/SimpleLayout.cs index 74fa81f..a02c56c 100644 --- a/simple-layout/SimpleLayout.cs +++ b/simple-layout/SimpleLayout.cs @@ -28,9 +28,10 @@ protected override void OnCreate() private void Initialize() { - // Change the background color of Window to White + // Change the background color of Window to White & respond to key events Window window = Window.Instance; window.BackgroundColor = Color.White; + window.KeyEvent += OnKeyEvent; // Create a new view View customLayoutView = new View(); @@ -75,6 +76,26 @@ ImageView CreateChildImageView( String url, Size2D size ) return imageView; } + /// + /// Called when any key event is received. + /// Will use this to exit the application if the Back or Escape key is pressed + /// + private void OnKeyEvent( object sender, Window.KeyEventArgs eventArgs ) + { + if( eventArgs.Key.State == Key.StateType.Down ) + { + switch( eventArgs.Key.KeyPressedName ) + { + case "Escape": + case "Back": + { + Exit(); + } + break; + } + } + } + static void Main(string[] args) { SimpleLayout simpleLayout = new SimpleLayout(); diff --git a/simple-text/simple-text.cs b/simple-text/simple-text.cs old mode 100755 new mode 100644 index 1101470..abdc32f --- a/simple-text/simple-text.cs +++ b/simple-text/simple-text.cs @@ -232,37 +232,48 @@ public void OnWindowKeyEvent(object sender, Window.KeyEventArgs e) { keySubclassTest.Text = $"DeviceSubClass={e.Key.DeviceSubClass}, DeviceClass={e.Key.DeviceClass}, DeviceName={e.Key.DeviceName}, KeyCode={e.Key.KeyCode}"; - if (e.Key.KeyPressedName == "Up") + switch( e.Key.KeyPressedName ) { - if (_animation) + case "Up": { - _animation.Finished += AnimationFinished; - cnt++; - Tizen.Log.Fatal("NUI", "AnimationFinished added!"); + if (_animation) + { + _animation.Finished += AnimationFinished; + cnt++; + Tizen.Log.Fatal("NUI", "AnimationFinished added!"); + } + + Tizen.Log.Fatal("NUI", $"LineWrapMode 1st={ myTextLabel?.LineWrapMode} 2nd={ myTextLabel2?.LineWrapMode}"); } - //pointLabel.TextColorAnimatable = Color.Blue; - //pixelLabel.TextColorAnimatable = Color.Blue; + break; - Tizen.Log.Fatal("NUI", $"LineWrapMode 1st={ myTextLabel?.LineWrapMode} 2nd={ myTextLabel2?.LineWrapMode}"); - } - else if (e.Key.KeyPressedName == "Down") - { - if (_animation) + case "Down": { - _animation.Finished -= AnimationFinished; - cnt--; - Tizen.Log.Fatal("NUI", "AnimationFinished removed!"); + if (_animation) + { + _animation.Finished -= AnimationFinished; + cnt--; + Tizen.Log.Fatal("NUI", "AnimationFinished removed!"); + } + + Window.Instance.SetClass($"Window.SetClass() Test #{win_test++}", ""); + Tizen.Log.Fatal("NUI", $"Check with enlightenment_info -topwins ! Window.SetClass() Test #{win_test}"); } - //pointLabel.TextColorAnimatable = Color.Red; - //pixelLabel.TextColorAnimatable = Color.Red; + break; - Window.Instance.SetClass($"Window.SetClass() Test #{win_test++}", ""); - Tizen.Log.Fatal("NUI", $"Check with enlightenment_info -topwins ! Window.SetClass() Test #{win_test}"); - } - else if (e.Key.KeyPressedName == "Return") - { - _animation.Play(); - Tizen.Log.Fatal("NUI", "_animation play here!"); + case "Return": + { + _animation.Play(); + Tizen.Log.Fatal("NUI", "_animation play here!"); + } + break; + + case "Escape": + case "Back": + { + Exit(); + } + break; } } }