I have coded an app in WPF where i have Keydown event functioning with the code below:
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. 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:
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?
Ok so when i involve the if (e.Key == Windows.System.VirtualKey.F) the code works as below:
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. So i have to incorporate something similar to UWP as i did in the WPF code below:
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 ...
`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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.