peerlessDJ
Topic Author
Posts: 34
Joined: 28 Jan 2019, 06:37

About KeyBoard Event

08 Jun 2020, 14:09

i want to listen Key Event on Image Element,so i implemented KeyDown,KeyUp and TextInput event.
%QQHYW%RFV9O`AD64`{R[CM.png
but in TextInput Event, English Char can't trigger it and Chinese char can. Why does this happen?
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: About KeyBoard Event

10 Jun 2020, 11:20

Hi, I did the following test and I'm receiving the key events (KeyDown, KeyUp and TextInput) for all keys pressed, not only for Chinese scripts.
<UserControl x:Class="Tests.ImageKeyEvents"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Image x:Name="TheImage" Source="Images/PanelBg.png" Stretch="None"/>
</UserControl>
struct ImageKeyEvents: public UserControl
{
    ImageKeyEvents()
    {
        InitializeComponent();

        Image* image = FindName<Image>("TheImage");
        image->SetFocusable(true);

        Loaded() += [image](BaseComponent*, const RoutedEventArgs&)
        {
            image->Focus();
        };

        image->KeyDown() += [](BaseComponent*, const KeyEventArgs& e)
        {
            NS_LOG_INFO("KeyDown %d %d", (int)e.key, (int)e.keyStates);
        };
        image->KeyUp() += [](BaseComponent*, const KeyEventArgs& e)
        {
            NS_LOG_INFO("KeyDown %d %d", (int)e.key, (int)e.keyStates);
        };
        image->TextInput() += [](BaseComponent*, const TextCompositionEventArgs& e)
        {
            char utf8Char[5] = { 0, 0, 0, 0, 0 };
            UTF8::Append(e.ch, utf8Char);

            NS_LOG_INFO("TextInput %s", utf8Char);
        };
    }

private:
    void InitializeComponent()
    {
        GUI::LoadComponent(this, "ImageTextEvents.xaml");
    }

    NS_IMPLEMENT_INLINE_REFLECTION_(ImageKeyEvents, UserControl, "Tests.ImageKeyEvents")
};
Perhaps you are focusing the image before it is loaded and connected to the UI tree?
Could you make sure image has keyboard focus when pressing the keys?
And remember that the TextInput event provides an unicode character and maybe you need to convert it to utf8 to use it (just as I did in my code).
 
peerlessDJ
Topic Author
Posts: 34
Joined: 28 Jan 2019, 06:37

Re: About KeyBoard Event

11 Jun 2020, 05:18

Hi, I did the following test and I'm receiving the key events (KeyDown, KeyUp and TextInput) for all keys pressed, not only for Chinese scripts.
<UserControl x:Class="Tests.ImageKeyEvents"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Image x:Name="TheImage" Source="Images/PanelBg.png" Stretch="None"/>
</UserControl>
struct ImageKeyEvents: public UserControl
{
    ImageKeyEvents()
    {
        InitializeComponent();

        Image* image = FindName<Image>("TheImage");
        image->SetFocusable(true);

        Loaded() += [image](BaseComponent*, const RoutedEventArgs&)
        {
            image->Focus();
        };

        image->KeyDown() += [](BaseComponent*, const KeyEventArgs& e)
        {
            NS_LOG_INFO("KeyDown %d %d", (int)e.key, (int)e.keyStates);
        };
        image->KeyUp() += [](BaseComponent*, const KeyEventArgs& e)
        {
            NS_LOG_INFO("KeyDown %d %d", (int)e.key, (int)e.keyStates);
        };
        image->TextInput() += [](BaseComponent*, const TextCompositionEventArgs& e)
        {
            char utf8Char[5] = { 0, 0, 0, 0, 0 };
            UTF8::Append(e.ch, utf8Char);

            NS_LOG_INFO("TextInput %s", utf8Char);
        };
    }

private:
    void InitializeComponent()
    {
        GUI::LoadComponent(this, "ImageTextEvents.xaml");
    }

    NS_IMPLEMENT_INLINE_REFLECTION_(ImageKeyEvents, UserControl, "Tests.ImageKeyEvents")
};
Perhaps you are focusing the image before it is loaded and connected to the UI tree?
Could you make sure image has keyboard focus when pressing the keys?
And remember that the TextInput event provides an unicode character and maybe you need to convert it to utf8 to use it (just as I did in my code).
Thank you. I solved my problem. Because in KeyDown, KeyUp, TextInput Delegate, i called “e.handle = true”

Who is online

Users browsing this forum: Semrush [Bot] and 29 guests