UWP KeyDown事件

时间:2018-01-14 11:19:25

标签: c# bluetooth uwp uwp-xaml

我在WPF中编写了一个应用程序,其中我使用以下代码运行Keydown事件:

private void Form1_Load(object sender, EventArgs e)
    {
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(Form1_KeyDown);
        this.KeyUp += new System.Windows.Forms.KeyEventHandler(Form1_KeyUp);
    }

    //Declare the comands for Rover control//
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)                                // Holding Keyboard Character "W" //
        {
            serialPort1.Write("F");                             // Passing command "Forward" thorugh letter "F" in arduino code //
        }

我试图在UWP中复制这个,但我不确定我做错了什么。 根据目前为止的研究,我知道我必须放置KeyDown =" Grid_KeyDown"在XAML部分的Grid中,我必须编写如下内容:

 private async void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        //handling code here
        if (e.Key == Key.F)
        {
            string sendData = "F";
            if (string.IsNullOrEmpty(sendData))
            {
                errorStatus.Visibility = Visibility.Visible;
                errorStatus.Text = "Please specify the string you are going to send";
            }
            else
            {
                DataWriter dwriter = new DataWriter(streamSocket.OutputStream);
                UInt32 len = dwriter.MeasureString(sendData);
                dwriter.WriteUInt32(len);
                dwriter.WriteString(sendData);
                await dwriter.StoreAsync();
                await dwriter.FlushAsync();
            }
        }

然而这不起作用。有没有人有任何建议如何让这个Keydown工作,所以当我按F键我想将它传递给串口并发送它作为字符串" F"到蓝牙设备?

好的,当我涉及if(e.Key == Windows.System.VirtualKey.F)时,代码的工作原理如下:

private async void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
    {

        //handling code here
        if (e.Key == Windows.System.VirtualKey.F)
        {
            string sendData = "F";
            if (string.IsNullOrEmpty(sendData))
            {
                errorStatus.Visibility = Visibility.Visible;
                errorStatus.Text = "Please specify the string you are going to send";
            }
            else
            {
                DataWriter dwriter = new DataWriter(streamSocket.OutputStream);
                UInt32 len = dwriter.MeasureString(sendData);
                dwriter.WriteUInt32(len);
                dwriter.WriteString(sendData);
                await dwriter.StoreAsync();
                await dwriter.FlushAsync();
            }
        }
    }

然而,我喜欢延迟。正如我在使用serialPort1.Write的代码的第一部分中提到的,当我按下按钮时,蓝牙将立即将命令发送到机器人,当我释放按钮时,蓝牙将立即停止。所以我必须像在下面的WPF代码中那样包含类似于UWP的东西:

 private void Form1_Load(object sender, EventArgs e)
    {
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(Form1_KeyDown);
        this.KeyUp += new System.Windows.Forms.KeyEventHandler(Form1_KeyUp);
    }

    //Declare the comands for Rover control//
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)                                // Holding Keyboard Character "W" //
        {
            serialPort1.Write("F");                             // Passing command "Forward" thorugh letter "F" in arduino code //
        }

谢谢

3 个答案:

答案 0 :(得分:0)

您是否尝试过按键事件如下所示:

<Grid KeyUp="Grid_KeyUp">
  ...
</Grid>

void Grid_KeyUp(object sender, KeyRoutedEventArgs e)
{
    //handling code here
}

有关详细信息,请阅读文档here

答案 1 :(得分:0)

也许这会有所帮助!这就是我使用KeyUP的方式......

`private void Value1_KeyUp(object sender, KeyRoutedEventArgs e)
  {
    if (e.Key == VirtualKey.Enter)
    {
        string value = Value1.Text;
        PivotItem pi = MainPivot.SelectedItem;
        ((WebView)pi.Content).Navigate(new Uri(value,
        UriKind.Absolute));   
        MainPivot.SelectedItem = pi;
        TagTextBlock.Text = Value1.Text;
        pi.Header = ((WebView)pi.Content).DocumentTitle;
    }
}

`

答案 2 :(得分:0)

也许您需要刷新DataWriter:

await dwriter.FlushAsync();