KeyDown处理C#/UWP [英] KeyDown Handling C# / UWP

查看:136
本文介绍了KeyDown处理C#/UWP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使应用向中继发送脉冲.我可以通过Button_click完成操作,但是现在我想通过任何击键操作完成操作.我可以用来做什么? 我了解了KeyDown,但我真的不知道如何集成它,任何帮助将不胜感激.预先谢谢你!

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!

这是我的代码的样子:

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;
        }

推荐答案

您可以将KeyDown事件侦听器添加到您的页面:

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");
}

这篇关于KeyDown处理C#/UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆