我正在尝试制作一个小型应用程序,该应用程序在按F键(在当前上下文中为F1,F2和F3)时执行特定功能。我最近才开始使用C#玩热键,但似乎无法弄清楚。我尝试将System.Windows.Input.KeyEventArgs更改为System.Windows.Forms.KeyEventArgs,但它不起作用。我不确定这是否是最好/正确的方法,但是从逻辑上讲,这对我来说很有意义。当其他F键发送文本命令时,activeTracker就像是我的循环的触发器。

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错误。

进行更改,并确保您明确引用System.Windows.InputKeyEventArgs版本,并且应该很好用。

09-26 07:57