gpt4 book ai didi

C# WPF 设置 F 热键

转载 作者:行者123 更新时间:2023-11-30 23:30:30 24 4
gpt4 key购买 nike

我正在尝试制作一个小型应用程序,在按下 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无论如何都是有值(value)的。

您的 KeyDown 处理程序无法编译,因为您使用了错误的变量名称 activateTracker 而不是 activeTracker

更改它并确保您显式引用 KeyEventArgsSystem.Windows.Input 版本,这样您就可以开始了。

关于C# WPF 设置 F 热键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34966732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com