C# WPF 设置 F 热键
本文关键字:热键 设置 WPF | 更新日期: 2023-09-27 18:31:45
我正在尝试制作一个小应用程序,在按下F键时执行特定功能(当前上下文中的F1,F2和F3)。我最近才开始使用 C# 中的热键,但我似乎无法弄清楚。我尝试将System.Windows.Input.KeyEventArgs追逐到System.Windows.Forms.KeyEventArgs,但它不起作用。我不确定这是否是最好/正确的方法,但是从逻辑上讲,这对我来说是有意义的。activeTracker 就像我的循环的触发器,而其他 F 键则发送文本命令。
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
bool activeTracker = false;
private void btnActive_Click(object sender, RoutedEventArgs e)
{
while (activeTracker)
{
IntPtr WindowHandle = FindWindow(txtClassName.Text, txtWindowTitle.Text);
if (WindowHandle == IntPtr.Zero)
{
System.Windows.MessageBox.Show(txtWindowTitle.Text + " does not exist");
return;
}
SetForegroundWindow(WindowHandle);
SendKeys.SendWait(txtMessage1.Text + "{ENTER}");
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
SendKeys.SendWait(txtMessage2.Text + "{ENTER}");
}
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.KeyCode == "F1")
{
activateTracker = True;
return;
}else if(e.KeyCode == "F2")
{
activateTracker = False;
return;
}else if(e.KeyCode == "F3")
{
SendKeys.SendWait(txtMessage5.Text + "{ENTER}");
}
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" KeyDown="Window_KeyDown">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="108*"/>
<ColumnDefinition Width="409*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="txtWindowTitle" HorizontalAlignment="Left" Height="23" Margin="176,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" Visibility="Hidden" Text="Value1" Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" Margin="33,130,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.Column="1"/>
<TextBox x:Name="txtClassName" HorizontalAlignment="Left" Height="23" Margin="176,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="233" Visibility="Hidden" Text="Value2" Grid.Column="1"/>
<Label Content="Message 1:" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top"/>
<Label Content="Message 2:" HorizontalAlignment="Left" Margin="10,68,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtMessage1" HorizontalAlignment="Left" Height="23" Margin="96,40,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
<Label Content="Message 3:" HorizontalAlignment="Left" Margin="10,99,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtMessage2" HorizontalAlignment="Left" Height="23" Margin="96,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtMessage3" HorizontalAlignment="Left" Height="23" Margin="96,102,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
<Button x:Name="btnActive" Content="Activate" HorizontalAlignment="Left" Margin="56,237,0,0" VerticalAlignment="Top" Width="75" Click="btnActive_Click" Grid.ColumnSpan="2"/>
<TextBox x:Name="txtMessage5" HorizontalAlignment="Left" Height="23" Margin="96,146,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" Grid.ColumnSpan="2"/>
</Grid>
</Window>
使用 Key
属性:
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
case Key.F1:
activeTracker= True;
break;
case Key.F2:
activeTracker= False;
break;
case Key.F3:
SendKeys.SendWait(txtMessage5.Text + "{ENTER}");
break;
}
我不确定您如何设法将 KeyCode
属性与字符串进行比较,因为 a) 它是 WinForms 和 b) 无论如何它都会返回Keys
值。
您的 KeyDown 处理程序无法编译,因为您activateTracker
使用了错误的变量名称,而不是 activeTracker
。
更改它并确保明确引用KeyEventArgs
的System.Windows.Input
版本,您应该很高兴。
您不需要设置它们。可以使用FrameworkElement.KeyDown
事件处理程序。
如果您的应用程序只有一个窗口,您可以直接将它们添加到代码中。
private void Window_KeyDown(object sender, KeyEventArgs e)
{
switch(e.Key)
{
// Add cases for F buttons.
}
}
但是,如果你的应用使用多个窗口,你可以使用它:
public static class KeyF
{
public static void RunF(KeyEventArgs e)
{
switch(e.Key)
{
// Add cases for F keys.
// And do stuff.
}
}
}
然后只需用KeyF.RunF(e);
运行它