其實本帖與this不同(我也發帖)。我在我的用戶界面中有一個電話鍵盤,現在我可以顯示相應按鍵關閉時按下的按鈕。如何在組合鍵的用戶界面中按下顯示按鈕
private void NumDisplayBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
case Key.NumPad0:
ZeroBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D1:
case Key.NumPad1:
OneBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D2:
case Key.NumPad2:
TwoBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D3:
case Key.NumPad3:
ThreeBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D4:
case Key.NumPad4:
FourBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D5:
case Key.NumPad5:
FiveBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D6:
case Key.NumPad6:
SixBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D7:
case Key.NumPad7:
SevenBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D8:
case Key.NumPad8:
EightBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D9:
case Key.NumPad9:
NineBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
}
if (((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift))
{
if (e.Key == Key.D3)
{
ThreeBtn.Style = (Style)FindResource("NormalButtonStyle");
HashBtn.Style = (Style)FindResource("PressedButtonStyle");
}
}
}
private void NumDisplayBox_PreviewKeyUp(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
case Key.NumPad0:
ZeroBtn.Style = (Style) FindResource("ButtonStyle4"); break;
case Key.D1:
case Key.NumPad1:
OneBtn.Style = (Style) FindResource("ButtonStyle4"); break;
.
.
.
}
}
問題是,當散列鍵(Shift鍵+ 3)或星號鍵(Shift鍵+ 8)爲關閉,則 '3' 鍵(用於殼體散列鍵)在UI也被按壓。如何顯示在Shift + 3關閉時僅在UI上按下哈希按鈕,而不是與「3」按鈕一起使用哈希按鈕?
嗨@PeterDuniho,如果我使用「的TextInput」或「PreviewTextInput」,我怎麼能抓住每一個個性我進去嗎?如果我設置'if(textbox.Text ==「#」',它必須是整個字符串是「#」,而不是字符 – YWah
@YWah:'TextInput'事件大致(非常粗略)類似於Winforms 'KeyPress'事件,通常情況下,字符串實際上只是一個字符,它是一個字符串,因爲一些輸入方法實際上可以生成多字符輸入,但大多數不會。像'e.Text ==「#」'而不是'e.Key == Key.D3 &&(e.Modifiers&ModifierKeys.Shift)!= 0'。但是這對我來說並不是問題。 –