Keyboard input
Posted: 22 Mar 2019, 22:44
Hi, I am trying to move a shape inside a canvas base on player keyboard input. but no matter what i try , the canvas does not receive the KeyDown event.
here is my code
here the code behind
neither of the two method work. I am surely missing something. can you please help me ? thank
here is my code
Code: Select all
<UserControl x:Class="HelloWorld.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
xmlns:local="clr-namespace:HelloWorld"
xmlns:noesis="clr-namespace:Noesis"
Background="#FF124C7A" >
<Canvas x:Name="CanvasContainer" KeyDown="Canvas_KeyDown" >
<Border x:Name="Player" BorderBrush="Black" BorderThickness="1" CornerRadius="8" Background="red" Margin="4" Padding="0" Width="80" Height="80"/>
</Canvas>
</UserControl>
Code: Select all
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
using UnityEngine;
using Canvas = Noesis.Canvas;
using GUI = Noesis.GUI;
#else
using System;
using System.Windows;
using System.Windows.Controls;
#endif
namespace HelloWorld
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : UserControl
{
public MainWindow()
{
this.Initialized += OnInitialized;
InitializeComponent();
}
#if NOESIS
public Canvas CanvasContainer;
public Border Player;
private void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "Assets/NoesisGUI/Samples/HelloWorld/MainWindow.xaml");
this.CanvasContainer = (Canvas)FindName("CanvasContainer");
this.Player = (Border)FindName("Player");
CanvasContainer.Focus();
Player.Focus();
}
#endif
private void OnInitialized(object sender, EventArgs args)
{
CanvasContainer.Focus();
Player.Focus();
//this.DataContext = new ViewModel();
}
protected override bool ConnectEvent(object source, string eventName, string handlerName)
{
if (eventName == "KeyDown" && handlerName == "Canvas_KeyDown")
{
((Noesis.Canvas)source).KeyDown += Canvas_KeyDown;
return true;
}
return false;
}
public void Canvas_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
#if NOESIS
Debug.Log("Canvas_KeyDown " + e.Key);
#else
Console.WriteLine("Canvas_KeyDown "+e.Key);
#endif
}
}
}
}