2012-04-07 57 views
2

我有兩個按鈕名稱「A」和「B」想單擊它們等於點擊鍵盤A和B按鈕。所以任何人都可以給我一個快速的方法來實現它。wpf按鈕點擊作爲鍵盤輸出

謝謝

+0

如。窗口中的兩個按鈕按鈕「a」和按鈕「b」。我打開一個文本文件。並點擊按鈕'a',文字也顯示出來。像虛擬鍵盤只有兩個鍵a和b。 – Jessesiu 2012-04-07 15:06:18

回答

1

我會這樣做。首先添加事件處理程序窗口中的按鍵事件或其他一些父容器:

<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" KeyDown="Window_KeyDown" Width="525"> [...] 

和事件處理程序:

private void Window_KeyDown(object sender, KeyEventArgs e) 
{ 
     if (e.Key == Key.A) 
      ButtonA.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); 
     else if (e.Key == Key.B) 
      ButtonB.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); 
} 
+0

謝謝我已經嘗試過。但它不能工作。我想單擊按鈕名稱'a',然後在doc文件或文本文件中顯示'a'。 – Jessesiu 2012-04-07 15:01:48