- peerlessDJ
- Posts: 34
- Joined:
About KeyBoard Event
i want to listen Key Event on Image Element,so i implemented KeyDown,KeyUp and TextInput event.
but in TextInput Event, English Char can't trigger it and Chinese char can. Why does this happen?-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: About KeyBoard Event
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.
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).
Code: Select all
<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>
Code: Select all
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")
};
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
- Posts: 34
- Joined:
Re: About KeyBoard Event
Thank you. I solved my problem. Because in KeyDown, KeyUp, TextInput Delegate, i called “e.handle = true”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.Code: Select all<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>
Perhaps you are focusing the image before it is loaded and connected to the UI tree?Code: Select allstruct 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") };
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).
Who is online
Users browsing this forum: Semrush [Bot] and 29 guests