Skip to content

Missing EventHandler usage #56

Description

@ticky

Hi there,

I’m trying to use the Windows.Gaming.Input methods to get Gamepad access. I’m having a lot of trouble figuring out how to implement the event handlers! I’ve gotten as far as getting an event handler with a signature that got accepted (kind of complicated, it seems!);

extern crate winrt;

use winrt::*;
use winrt::windows::foundation::*;
use winrt::windows::gaming::input::*;

fn main() {
    let rt = RuntimeContext::init();
    run();
    rt.uninit();
}

fn added(_sender: *mut winrt::IInspectable, gamepad: *mut Gamepad) -> Result<()> {
    print!("Gamepad added");

    Ok(())
}

fn run() {
    let gamepad_list = Gamepad::get_gamepads().unwrap().unwrap();

    print!("{} gamepads connected", gamepad_list.into_iter().count());

    match Gamepad::add_gamepad_added(&<EventHandler<Gamepad>>::new(added)) {
        Error => panic!("Couldn't add event handler")
    }

    for (_index, gamepad) in gamepad_list.into_iter().enumerate() {
        print!("{:?}", gamepad.unwrap().get_current_reading().unwrap());
    };
}

Unfortunately, I get “0 gamepads connected” printed, before it panics with not being able to add the event handler.

Is there something I’ve missed in how this is intended to work? I can find references in issues to event handlers, but I can’t see any example code.

Any help would be very appreciated - I’d also be happy to contribute the above in a PR as an example if I can get it working :)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions