2016-01-23 104 views
1

我試圖做一個小的應用程序,當F功能鍵被按下(F1,F2 F3 &在目前情況下)執行特定的功能。我剛剛在C#中開始使用熱鍵,但似乎無法弄清楚。我嘗試將System.Windows.Input.KeyEventArgs chaning到System.Windows.Forms.KeyEventArgs,但它不起作用。我不知道這是否是最好的/正確的方式來做到這一點,但從邏輯上來說,這對我來說是有意義的。 activeTracker就像我的循環觸發器,而其他F鍵發送文本命令。C#WPF設置˚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> 
+0

您是否在'Window_KeyDown'事件處理程序中設置了斷點以確保它被調用?你不顯示XAML,所以我只能假設你在那裏添加'KeyDown'事件。 – ChrisF

+0

將我的XAML代碼添加到我的文章中,不太清楚您設置斷點的含義。 – HereToLearn

+0

使用Visual Studio調試器 - 「F5」。在想要代碼停止的行上按「F9」,以便可以檢查變量並檢查它們是否是您期望它們的值。 – ChrisF

回答

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屬性用於字符串作爲比較)這是WinForms和b)也無妨返回Keys值。

您的KeyDown處理程序無法編譯,因爲您的變量名稱爲activateTracker而不是activeTracker

更改並確保您明確引用KeyEventArgsSystem.Windows.Input版本,並且應該很好。

+0

當我使用下面的switch語句時,出於某種原因,當前上下文中不存在activateTracker。儘管它是一個全局變量。 – HereToLearn

+1

@HereToLearn只要你用這段代碼替換了現有的KeyDown事件處理程序,那麼我看不出爲什麼它不應該編譯。 – ChrisF

+0

是的,但是當我使用下面的代碼KeyEventArgs要求我將其更改爲System.Windows.Input.KeyEventArgs或System.Windows.Forms.KeyEventArgs。當使用輸入時,無法找到actionTracker變量,當使用表單時,「密鑰」不包含定義。我在這裏錯過了什麼? – HereToLearn