Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to subscribe to keyboard events using a UWP application and C++?

I am using UWP with DirectX 11 for a game but I can't seem to find any examples on how to detect key presses with the windows events.

I have looked at the GetAsyncKeyState but for some reason this doesn't work even though I include the correct files and libraries, and I think for my application it would be much better if the input was event based.

Does anyone have any examples on how to recognize keyboard input using the windows events (https://docs.microsoft.com/en-us/uwp/api/windows.ui.core.corewindow.keydown)

The events work if they are in the default class of App.cpp but when I attempt to move it into another class, I get the errors.

Here is my class that I want to handle the input events

ref class InputManager {
internal:
    InputManager(Windows::UI::Core::CoreWindow^ window) {
        window->KeyDown += ref new TypedEventHandler<Windows::UI::Core::CoreWindow^, Windows::UI::Core::KeyEventArgs^>(this, &InputManager::OnKeyDown);
    }

private:
    void OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args) {

    }
};

Here are the errors

2>f:\(6): error C2061: syntax error: identifier 'TypedEventHandler' (compiling source file App.cpp)
2>f:\(6): error C2061: syntax error: identifier 'TypedEventHandler' (compiling source file )
2>f:\(6): error C2061: syntax error: identifier 'TypedEventHandler' (compiling source file )
2>f:\ (49): error C2664: 'Windows::Foundation::EventRegistrationToken Windows::ApplicationModel::Core::CoreApplicationView::Activated::add(Windows::Foundation::TypedEventHandler<Windows::ApplicationModel::Core::CoreApplicationView ^,Windows::ApplicationModel::Activation::IActivatedEventArgs ^> ^)': cannot convert argument 1 from 'Windows::Foundation::EventHandler<Windows::ApplicationModel::SuspendingEventArgs ^> ^' to 'Windows::Foundation::TypedEventHandler<Windows::ApplicationModel::Core::CoreApplicationView ^,Windows::ApplicationModel::Activation::IActivatedEventArgs ^> ^'
2>f:\ (50): note: No user-defined-conversion operator available, or
2>f:\ (50): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>f:\ (52): error C2664: 'Windows::Foundation::EventRegistrationToken Windows::ApplicationModel::Core::CoreApplication::Suspending::add(Windows::Foundation::EventHandler<Windows::ApplicationModel::SuspendingEventArgs ^> ^)': cannot convert argument 1 from 'Windows::Foundation::EventHandler<Platform::Object ^> ^' to 'Windows::Foundation::EventHandler<Windows::ApplicationModel::SuspendingEventArgs ^> ^'
2>f:\ (53): note: No user-defined-conversion operator available, or
2>f:\ (53): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>f:\ (63): error C2664: 'Windows::Foundation::EventRegistrationToken Windows::ApplicationModel::Core::CoreApplication::Resuming::add(Windows::Foundation::EventHandler<Platform::Object ^> ^)': cannot convert argument 1 from 'Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^,Windows::UI::Core::WindowSizeChangedEventArgs ^> ^' to 'Windows::Foundation::EventHandler<Platform::Object ^> ^'
2>f:\ (64): note: No user-defined-conversion operator available, or
2>f:\ (64): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>f:\ (66): error C2664: 'Windows::Foundation::EventRegistrationToken Windows::UI::Core::CoreWindow::SizeChanged::add(Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^,Windows::UI::Core::WindowSizeChangedEventArgs ^> ^)': cannot convert argument 1 from 'Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^,Windows::UI::Core::VisibilityChangedEventArgs ^> ^' to 'Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^,Windows::UI::Core::WindowSizeChangedEventArgs ^> ^'
2>f:\ (67): note: No user-defined-conversion operator available, or

When I remove the line that adds the event in the constructor, everything compiles fine.

Comments