KeyDown Handling C# / UWP

南楼画角 提交于 2019-12-11 15:29:27

问题


I'm Trying to make app send a pulse to a relay. I got it done with a Button_click, but now I want to do it with any keystroke. What Can I use for this? I read about KeyDown but I don't really know how to integrate it, Any help would be much appreciated. Thank you in advance!

Here is how my code looks like:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (relay_state == 0)
        {
            relayPin.Write(GpioPinValue.Low);
            relay_state = 1;
        }
        else if (relay_state == 1)
        {
            relayPin.Write(GpioPinValue.High);
            relay_state = 0;
        }

回答1:


You can add KeyDown event listener to your page:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    CoreWindow.GetForCurrentThread().KeyDown += Page_KeyDown;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    CoreWindow.GetForCurrentThread().KeyDown -= Page_KeyDown;
}

private void Page_KeyDown(CoreWindow sender, KeyEventArgs args)
{
    //Do your stuff here!
    System.Diagnostics.Debug.WriteLine("Pressed Key");
}


来源:https://stackoverflow.com/questions/45950918/keydown-handling-c-sharp-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!