簡體   English   中英

UWP KeyDown事件

[英]UWP KeyDown Event

我在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中復制此內容,但不確定我在做什么錯。 根據到目前為止的研究,我了解到我必須在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();
            }
        }

但是,這不起作用。 有沒有人有任何建議我如何使此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 //
        }

謝謝

您是否嘗試過使用如下所示的按鍵事件:

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

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

有關更多詳細信息,請閱讀此處的文檔。

也許這會有所幫助! 這就是我使用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