0
votes

I want to bind to Window::Current->CoreWindow->GetForCurrentThread()->KeyDown event (https://docs.microsoft.com/en-us/uwp/api/windows.ui.core.corewindow.keydown?view=winrt-19041). I tried:

Window::Current->CoreWindow->GetForCurrentThread()->KeyDown += Event_KeyDown;

void View3D::Event_KeyDown(Platform::Object^, Windows::UI::Xaml::Input::KeyRoutedEventArgs^)
{
    OutputDebugString(L"HERE\r\n");
}

but I have an error:

C++ function cannot be called with the given argument list
argument types are: (void (Platform::Object ^, Windows::UI::Xaml::Input::KeyRoutedEventArgs ^e)) object type is: Windows::UI::Core::CoreWindow ^

1
Hi, can the following method help? Have you solved your issue? - Faywang - MSFT
Yes, I have solved it, thanks :) - michalt38

1 Answers

0
votes

First, the parameters of KeyDown event are CoreWindow and KeyEventArgs, then when you try to subscribe the KeyDown event, you need to provide a handler function. For example:

.h:

void Event_KeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ e);

.cpp:

MainPage::MainPage()
{
    InitializeComponent();

    Window::Current->CoreWindow->GetForCurrentThread()->KeyDown += ref new Windows::Foundation::TypedEventHandler< Windows::UI::Core::CoreWindow^, Windows::UI::Core::KeyEventArgs^>(this, &MainPage::Event_KeyDown);

}

void MainPage::Event_KeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^e)
{
    OutputDebugString(L"HERE\r\n");
}

For more information about Events in C++/CX, you can refer to this document.