我有一个 Main_Form_Keydown和Keyup功能,用键盘控制机器人。 问题 如果我失去了主窗口窗口的焦点我无法再控制机器人,除非我再次关注 ,这是一个问题,因为我这样做可能会使机器人崩溃。
我有什么想法可以改进我的代码吗?
public void Main_Form_KeyDown(object sender, KeyEventArgs e) //Controlo do drone com o teclado
{
switch (e.KeyCode) //Controlo pelo Teclado
{
case Keys.F:
_droneClient.FlatTrim();
break;
case Keys.Back:
if (!isSensitiveMode)
{
flightSensitivityConst = 0.25f;
isSensitiveMode = true;
}
else
{
flightSensitivityConst = 0.9f;
isSensitiveMode = false;
}
break;
case Keys.W:
_droneClient.Progress(FlightMode.Progressive, pitch: -flightSensitivityConst);
break;
case Keys.S:
_droneClient.Progress(FlightMode.Progressive, pitch: +flightSensitivityConst);
break;
case Keys.A:
_droneClient.Progress(FlightMode.Progressive, roll: -flightSensitivityConst);
break;
case Keys.D:
_droneClient.Progress(FlightMode.Progressive, roll: +flightSensitivityConst);
break;
case Keys.I:
_droneClient.Progress(FlightMode.Progressive, gaz: +flightSensitivityConst);
break;
case Keys.K:
_droneClient.Progress(FlightMode.Progressive, gaz: -flightSensitivityConst);
break;
case Keys.J:
_droneClient.Progress(FlightMode.Progressive, yaw: -flightSensitivityConst);
break;
case Keys.L:
_droneClient.Progress(FlightMode.Progressive, yaw: +flightSensitivityConst);
break;
case Keys.E:
_droneClient.Takeoff();
break;
case Keys.Q:
_droneClient.Emergency();
break;
case Keys.Space:
_droneClient.Land();
break;
//FLIPS
case Keys.P:
_settings.Control.FlightAnimation = new FlightAnimation(FlightAnimationType.FlipLeft);
_droneClient.Send(_settings);
//FLIP
break;
case Keys.O:
_settings.Control.FlightAnimation = new FlightAnimation(FlightAnimationType.FlipRight);
_droneClient.Send(_settings);
//FLIP
break;
case Keys.Y:
_settings.Control.FlightAnimation = new FlightAnimation(FlightAnimationType.FlipBehind);
_droneClient.Send(_settings);
break;
case Keys.U:
_settings.Control.FlightAnimation = new FlightAnimation(FlightAnimationType.FlipAhead);
_droneClient.Send(_settings);
break;
case Keys.C:
var configuration = new Settings();
configuration.Video.Channel = VideoChannelType.Next;
_droneClient.Send(configuration);
break;
case Keys.R:
string path = string.Format("ttu_flight_{0:yyyy_MM_dd_HH_mm}" + ARDroneTrackFileExt, DateTime.Now);
using (var dialog = new SaveFileDialog { DefaultExt = ARDroneTrackFileExt, Filter = ARDroneTrackFilesFilter, FileName = path })
{
if (dialog.ShowDialog(this) == DialogResult.OK)
{
StopRecording();
_recorderStream = new FileStream(dialog.FileName, FileMode.OpenOrCreate);
_packetRecorderWorker = new PacketRecorder(_recorderStream);
_packetRecorderWorker.Start();
}
}
break;
}
} //Controlo do drone através do teclado
private void Main_Form_KeyUp(object sender, KeyEventArgs e) //Se nao estiver a ser comandado pelo teclado
{
_droneClient.Hover(); //Fica pairado
}
我正在考虑在计时器上使用普通函数,但它会缺少KeyEventArgs e
答案 0 :(得分:0)
使用全局密钥钩子。当您的窗口没有焦点时,这也会捕获键(请参阅:http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook或Global keyboard capture in C# application)。
或者,您可以捕获表单的LostFocus
事件,然后中断程序,直到表单再次聚焦为止。