简体   繁体   English

UWP KeyDown事件

[英]UWP KeyDown Event

I have coded an app in WPF where i have Keydown event functioning with the code below: 我在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 //
        }

I am trying to replicate this in UWP and am not sure what I am doing wrong. 我正在尝试在UWP中复制此内容,但不确定我在做什么错。 Based on research so far, I understand that i have to place KeyDown="Grid_KeyDown" within Grid in XAML part, and i have to write something like: 根据到目前为止的研究,我了解到我必须在XAML部分的Grid中放置KeyDown =“ Grid_KeyDown”,并且我必须编写如下内容:

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

However this is not working. 但是,这不起作用。 Does anyone have any suggestion how do I make this Keydown even work so when i press F key i want to pass that to serial port and send it as string "F" to Bluetooth device? 有没有人有任何建议我如何使此Keydown正常工作,所以当我按F键时我想将其传递到串行端口并将其作为字符串“ F”发送到蓝牙设备?

Ok so when i involve the if (e.Key == Windows.System.VirtualKey.F) the code works as below: 好的,所以当我涉及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();
            }
        }
    }

However, i am having like the delay. 但是,我喜欢延迟。 As i mentioned in the first part of the code where I was using serialPort1.Write, the Bluetooth will send the command to the robot instantaneously when i press button and will stop right away when i release the button. 正如我在使用serialPort1.Write的代码的第一部分中提到的那样,当我按下按钮时,蓝牙会立即将命令发送给机器人,而当我释放按钮时,蓝牙将立即停止。 So i have to incorporate something similar to UWP as i did in the WPF code below: 因此,我必须像在以下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 //
        }

Thank you 谢谢

Have you tried using key up event as below: 您是否尝试过使用如下所示的按键事件:

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

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

For more details read the documentation here . 有关更多详细信息,请阅读此处的文档。

Maybe this will help! 也许这会有所帮助! This is how I used KeyUP ... 这就是我使用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;
    }
}

` `

也许您需要刷新DataWriter:

await dwriter.FlushAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM