【问题标题】:C# WPF Setting Up F HotKeysC# WPF 设置 F 热键
【发布时间】:2016-04-30 05:39:01
【问题描述】:

我正在尝试制作一个在按下 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>

【问题讨论】:

  • 您是否在Window_KeyDown 事件处理程序中设置了断点以确保它被调用?您没有显示 XAML,所以我只能假设您正在那里添加 KeyDown 事件。
  • 将我的 XAML 代码添加到我的帖子中,不太清楚设置断点是什么意思。
  • 使用 Visual Studio 调试器 - “F5”。在您希望代码停止的行上按“F9”,这样您就可以检查变量并检查它们是否是您期望的值。

标签: c# wpf hotkeys


【解决方案1】:

使用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

更改它并确保您明确引用KeyEventArgsSystem.Windows.Input 版本,您应该一切顺利。

【讨论】:

  • 当我使用以下switch语句时,由于某种原因,当前上下文中不存在activateTracker。即使它是一个全局变量。
  • @HereToLearn 只要您刚刚用此代码替换了现有的 KeyDown 事件处理程序,那么我看不出它为什么不应该编译。
  • 是的,但是当我使用以下代码时,KeyEventArgs 要求我将其更改为 System.Windows.Input.KeyEventArgs 或 System.Windows.Forms.KeyEventArgs。使用 Input 时,找不到 actionTracker 变量,而使用 Forms 时,“Key”不包含定义。我在这里错过了什么?
  • @HereToLearn 这听起来不对。如果您的处理程序在您的 MainWindow 类中,它应该可以编译。
  • 'KeyEventArgs' 是 'System.Windows.Input.KeyEventArgs' 和 'System.Windows.Forms.KeyEventArgs' 之间的模糊引用是我收到的错误。看起来像图书馆参考问题。
【解决方案2】:

您无需设置它们。您可以使用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);运行它

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多